/** * @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; }
/** * @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; }
/** * @param Documents $documents * @return Collection * @depends testDocumentsApi */ public function testCollectionAutoloadOff(Documents $documents) { $criteria = new SearchEstimatesCriteria(); $criteria->setCreatePeriod(new \DateTime('@1457478000'), new \DateTime('@1457564400')); $paginator = new Paginator(); $paginator->setNumberPerPage(10); $paginator->setPageNumber(1); $estimates = $documents->searchEstimates($criteria, $paginator); $estimatesCount = 0; /** @var EstimateInterface $estimate */ foreach ($estimates as $estimate) { $estimatesCount++; } $this->assertEquals(10, $estimatesCount); return $estimates; }
/** * @inheritdoc */ public function count() { return $this->paginator->getNumberOfResults(); }