Browse Source

Update PhyreServer.php

Bozhidar Slaveykov 1 year ago
parent
commit
3a3b428a9e
1 changed files with 58 additions and 31 deletions
  1. 58 31
      web/app/Models/PhyreServer.php

+ 58 - 31
web/app/Models/PhyreServer.php

@@ -33,61 +33,88 @@ class PhyreServer extends Model
     public function syncResources()
     public function syncResources()
     {
     {
         // Sync customers
         // Sync customers
-        $customerExternalIds = [];
-        $getCustomers = Customer::where('phyre_server_id', $this->id)->get();
-        if ($getCustomers->count() > 0) {
-            foreach ($getCustomers as $customer) {
-                $customerExternalIds[] = $customer->external_id;
+        $centralServerCustomerExternalIds = [];
+        $getCentralServerCustomers = Customer::where('phyre_server_id', $this->id)->get();
+        if ($getCentralServerCustomers->count() > 0) {
+            foreach ($getCentralServerCustomers as $customer) {
+                $centralServerCustomerExternalIds[] = $customer->external_id;
             }
             }
         }
         }
+
         $phyreApiSDK = new PhyreApiSDK($this->ip, 8443, $this->username, $this->password);
         $phyreApiSDK = new PhyreApiSDK($this->ip, 8443, $this->username, $this->password);
-        $getExternalCustomers = $phyreApiSDK->getCustomers();
-        if (isset($getExternalCustomers['data']['customers'])) {
-            $externalCustomerIds = [];
-            foreach ($getExternalCustomers['data']['customers'] as $externalCustomer) {
-                $externalCustomerIds[] = $externalCustomer['id'];
+        $getPhyreServerCustomers = $phyreApiSDK->getCustomers();
+        if (isset($getPhyreServerCustomers['data']['customers'])) {
+            $phyreServerCustomerIds = [];
+            foreach ($getPhyreServerCustomers['data']['customers'] as $customer) {
+                $phyreServerCustomerIds[] = $customer['id'];
             }
             }
 
 
             // Delete customers to main server that are not in external server
             // Delete customers to main server that are not in external server
-            foreach ($customerExternalIds as $customerExternalId) {
-                if (!in_array($customerExternalId, $externalCustomerIds)) {
-                    $getCustomer = Customer::where('external_id', $customerExternalId)->first();
+            foreach ($centralServerCustomerExternalIds as $centralServerCustomerExternalId) {
+                if (!in_array($centralServerCustomerExternalId, $phyreServerCustomerIds)) {
+                    $getCustomer = Customer::where('external_id', $centralServerCustomerExternalId)
+                        ->where('phyre_server_id', $this->id)
+                        ->first();
                     if ($getCustomer) {
                     if ($getCustomer) {
                         $getCustomer->delete();
                         $getCustomer->delete();
                     }
                     }
                 }
                 }
             }
             }
+
             // Add customers to main server from external server
             // Add customers to main server from external server
-            foreach ($getExternalCustomers['data']['customers'] as $externalCustomer) {
-                $findCustomer = Customer::where('external_id', $externalCustomer['id'])
+            foreach ($getPhyreServerCustomers['data']['customers'] as $phyreServerCustomer) {
+                $findCustomer = Customer::where('external_id', $phyreServerCustomer['id'])
                     ->where('phyre_server_id', $this->id)
                     ->where('phyre_server_id', $this->id)
                     ->first();
                     ->first();
                 if (!$findCustomer) {
                 if (!$findCustomer) {
                     $findCustomer = new Customer();
                     $findCustomer = new Customer();
                     $findCustomer->phyre_server_id = $this->id;
                     $findCustomer->phyre_server_id = $this->id;
-                    $findCustomer->external_id = $externalCustomer['id'];
+                    $findCustomer->external_id = $phyreServerCustomer['id'];
                 }
                 }
-                $findCustomer->name = $externalCustomer['name'];
-                $findCustomer->username = $externalCustomer['username'];
-                $findCustomer->email = $externalCustomer['email'];
-                $findCustomer->phone = $externalCustomer['phone'];
-                $findCustomer->address = $externalCustomer['address'];
-                $findCustomer->city = $externalCustomer['city'];
-                $findCustomer->state = $externalCustomer['state'];
-                $findCustomer->zip = $externalCustomer['zip'];
-                $findCustomer->country = $externalCustomer['country'];
-                $findCustomer->company = $externalCustomer['company'];
+                $findCustomer->name = $phyreServerCustomer['name'];
+                $findCustomer->username = $phyreServerCustomer['username'];
+                $findCustomer->email = $phyreServerCustomer['email'];
+                $findCustomer->phone = $phyreServerCustomer['phone'];
+                $findCustomer->address = $phyreServerCustomer['address'];
+                $findCustomer->city = $phyreServerCustomer['city'];
+                $findCustomer->state = $phyreServerCustomer['state'];
+                $findCustomer->zip = $phyreServerCustomer['zip'];
+                $findCustomer->country = $phyreServerCustomer['country'];
+                $findCustomer->company = $phyreServerCustomer['company'];
                 $findCustomer->saveQuietly();
                 $findCustomer->saveQuietly();
             }
             }
         }
         }
 
 
-
         // Sync Hosting Subscriptions
         // Sync Hosting Subscriptions
-        $getExternalHostingSubscriptions = $phyreApiSDK->getHostingSubscriptions();
-        if (isset($getExternalHostingSubscriptions['data']['hostingSubscriptions'])) {
-            dd($getExternalHostingSubscriptions['data']['hostingSubscriptions']);
+        $centralServerHostingSubscriptionsExternalIds = [];
+        $getCentralHostingSubscriptions = HostingSubscription::where('phyre_server_id', $this->id)->get();
+        if ($getCentralHostingSubscriptions->count() > 0) {
+            foreach ($getCentralHostingSubscriptions as $customer) {
+                $centralServerHostingSubscriptionsExternalIds[] = $customer->external_id;
+            }
+        }
+      //  $getPhyreServerHostingSubscriptions = $phyreApiSDK->getHostingSubscriptions();
+
+        dd($centralServerHostingSubscriptionsExternalIds);
+
+        if (isset($getExternalHostingSubscriptions['data']['HostingSubscriptions'])) {
+            $externalHostingSubscriptionsIds = [];
+            foreach ($getExternalHostingSubscriptions['data']['HostingSubscriptions'] as $externalHostingSubscriptions) {
+                $externalHostingSubscriptionsIds[] = $externalHostingSubscriptions['id'];
+            }
+
+            // Delete hosting subscriptions to main server that are not in external server
+            foreach ($customerExternalIds as $customerExternalId) {
+                if (!in_array($customerExternalId, $externalCustomerIds)) {
+                    $getCustomer = Customer::where('external_id', $customerExternalId)->first();
+                    if ($getCustomer) {
+                        $getCustomer->delete();
+                    }
+                }
+            }
+
         }
         }
-        dd($getExternalHostingSubscriptions);
+        dd($externalHostingSubscriptionsIds);
 
 
 
 
 //        // Sync Hosting Plans
 //        // Sync Hosting Plans