Exemplo n.º 1
0
 /**
  * Sync the local cache with the remote changes. If checkpoint is null, this
  * will sync all remote node data.
  *
  * @throws \Exception
  */
 public function sync()
 {
     $params = ['maxNodes' => 5000];
     if ($this->checkpoint) {
         $params['includePurged'] = "true";
     }
     while (true) {
         if ($this->checkpoint) {
             $params['checkpoint'] = $this->checkpoint;
         }
         $loop = true;
         $response = $this->httpClient->post("{$this->getMetadataUrl()}changes", ['headers' => ['Authorization' => "Bearer {$this->token['access_token']}"], 'body' => json_encode($params), 'exceptions' => false]);
         if ($response->getStatusCode() !== 200) {
             throw new \Exception((string) $response->getBody());
         }
         $data = explode("\n", (string) $response->getBody());
         foreach ($data as $part) {
             $part = json_decode($part, true);
             if (isset($part['end']) && $part['end'] === true) {
                 break;
             }
             if (isset($part['reset']) && $part['reset'] === true) {
                 $this->cache->deleteAllNodes();
             }
             if (isset($part['nodes'])) {
                 if (empty($part['nodes'])) {
                     $loop = false;
                 } else {
                     foreach ($part['nodes'] as $node) {
                         $node = new Node($node);
                         if ($node['status'] === 'PURGED') {
                             $node->delete();
                         } else {
                             $node->save();
                         }
                     }
                 }
             }
             if (isset($part['checkpoint'])) {
                 $this->checkpoint = $part['checkpoint'];
             }
             $this->save();
         }
         if (!$loop) {
             break;
         }
     }
 }