Exemplo n.º 1
0
 /**
  * @return LocalTransport|TransportInterface
  */
 public static function getTransport()
 {
     if (!self::$transport) {
         /** @noinspection PhpUndefinedConstantInspection */
         self::$transport = new LocalTransport(CACHE_ENABLED);
         if ($realTransport = self::getRealTransport()) {
             self::$transport->setRealTransport($realTransport);
         }
     }
     return self::$transport;
 }
Exemplo n.º 2
0
 /**
  * @param $method
  * @param CriteriaInterface|null $criteria
  * @param Paginator|null $paginator
  * @return mixed
  * @throws \Sellsy\Exception\RuntimeException
  * @throws \Exception
  */
 public function call($method, CriteriaInterface $criteria = null, Paginator $paginator = null)
 {
     // Ensure to clean subject, @see finally
     try {
         $parameters = array_merge($criteria ? $criteria->getParameters() : array(), $paginator ? $paginator->getParameters() : array());
         $this->logger->debug(sprintf('API Call - Method %s', $method), array('parameters' => $parameters, 'subject' => $this->subject, 'context' => $this->context));
         // Send API Call with the transport
         $apiResult = $this->transport->call(array('method' => $method, 'params' => $parameters));
         $this->logger->debug(sprintf('API Result - Method %s', $method), array('result' => $apiResult));
         // API Call that return only a status
         if (array_key_exists('response', $apiResult) && !is_array($apiResult['response'])) {
             $this->subject = null;
             $this->context = null;
             return true;
         }
         // In this case, the subject and context is required for this adapter
         if (!$this->subject || !$this->context) {
             throw new RuntimeException('No interface mapped or no context defined, you must call "map" method before use the "call" method');
         }
         // API Call that return a collection
         if (isset($apiResult['response']['result'])) {
             // Update paginator from API Response
             $paginator = $paginator ?: new Paginator();
             if (isset($apiResult['response']['infos'])) {
                 $paginator->setPageNumber($apiResult['response']['infos']['pagenum']);
                 $paginator->setNumberPerPage($apiResult['response']['infos']['nbperpage']);
                 $paginator->setNumberOfPages($apiResult['response']['infos']['nbpages']);
                 $paginator->setNumberOfResults($apiResult['response']['infos']['nbtotal']);
             } else {
                 $paginator->setPageNumber(1);
                 $paginator->setNumberOfPages(1);
                 $paginator->setNumberOfResults(count($apiResult['response']['result']));
                 $paginator->setNumberPerPage($paginator->getNumberOfResults());
             }
             // Initialize items
             $items = array();
             // Map objects
             foreach ($apiResult['response']['result'] as $value) {
                 $items[] = $this->mapper->mapObject($this->subject, $this->context, $value);
             }
             $result = new Collection(array('items' => $items, 'adapter' => $this, 'method' => $method, 'subject' => $this->subject, 'context' => $this->context, 'paginator' => $paginator, 'criteria' => $criteria));
         } else {
             $result = $this->mapper->mapObject($this->subject, $this->context, $apiResult['response']);
         }
     } catch (\Exception $e) {
         throw $e;
     } finally {
         $this->subject = null;
         $this->context = null;
     }
     return $result;
 }
Exemplo n.º 3
0
 /**
  * @param array $requestSettings
  * @return array
  * @throws \Sellsy\Exception\ServerException
  */
 public function call(array $requestSettings)
 {
     if (!$this->useCache) {
         return $this->realTransport->call($requestSettings);
     }
     $path = $this->getLocalFileName($requestSettings);
     if (file_exists($path)) {
         return json_decode(file_get_contents($path), true);
     }
     if ($this->realTransport) {
         $array = $this->realTransport->call($requestSettings);
         $directory = dirname($path);
         if (!is_dir($directory) && !mkdir($directory, 0755, true)) {
             throw new \RuntimeException("Unable create directory " . $directory);
         }
         if (!file_put_contents($path, $this->getAnonymizeJson($array))) {
             throw new \RuntimeException("Unable to save data file " . $path);
         }
         return json_decode(file_get_contents($path), true);
     }
     throw new \RuntimeException("Unable to find the data file " . $path);
 }
Exemplo n.º 4
0
 /**
  * @inheritdoc
  */
 public function call($method, CriteriaInterface $criteria = null, Paginator $paginator = null)
 {
     $parameters = array_merge($criteria ? $criteria->getParameters() : array(), $paginator ? $paginator->getParameters() : array());
     $this->logger->debug(sprintf('API Call - Method %s', $method), array('parameters' => $parameters));
     $result = $this->transport->call(array('method' => $method, 'params' => $parameters));
     $this->logger->debug(sprintf('API Result - Method %s', $method), array('result' => $result));
     // API Call that return a collection
     if (isset($result['response']['result'])) {
         // Update paginator from API Response
         $paginator = $paginator ?: new Paginator();
         $paginator->setPageNumber($result['response']['infos']['pagenum']);
         $paginator->setNumberPerPage($result['response']['infos']['nbperpage']);
         $paginator->setNumberOfPages($result['response']['infos']['nbpages']);
         $paginator->setNumberOfResults($result['response']['infos']['nbtotal']);
         // Initialize items
         $items = array();
         // Map objects
         foreach ($result['response']['result'] as $value) {
             $items[] = $value;
         }
         $result = new Collection(array('items' => $items, 'adapter' => $this, 'method' => $method, 'paginator' => $paginator, 'criteria' => $criteria));
     }
     return $result;
 }