public function execute()
 {
     $this->hasExecuted = true;
     $this->items = [];
     /** @var integer $pageNum */
     $pageNum = 1;
     /** @var integer $numberResults */
     $numberResults = 0;
     /** @var int $pageSize */
     if (!$this->getLimit() || $this->getLimit() > self::MAX_PAGE_LIMIT) {
         $pageSize = self::MAX_PAGE_LIMIT;
     } else {
         $pageSize = $this->getLimit();
     }
     /** @var string[] $params */
     $params = array_merge($this->entity->getDefaultQueryParams(), $this->filters);
     $params['page'] = $pageNum;
     $params['per_page'] = $pageSize;
     if ($this->getSortBy()) {
         $params['sort'] = ($this->getSortDir() == self::SORT_DESC ? '-' : '') . $this->getSortBy();
     }
     /** @var string $resultsKey */
     $resultsKey = $this->entity->getApiEntityCollectionName();
     //sprintf('%ss', $this->entity->getApiEntityName());
     while (true) {
         // clip last page if it exceeds requested result limit
         if ($this->getLimit() && $this->getLimit() - $numberResults < $pageSize) {
             $params['per_page'] = $this->getLimit() - $numberResults;
         }
         /** @var string $url */
         $url = $this->api->getUrl($this->entity->getApiResourceName(), $params);
         /** @var mixed[] $response */
         $response = $this->api->GET($url);
         $response = $this->entity->parseCollectionResponse($response);
         if (!isset($response[$resultsKey])) {
             throw new Exception("{$resultsKey} could not be found in query result");
         }
         /** @var mixed[] $data */
         $data = $response[$resultsKey];
         /** @var mixed[] $entityData */
         foreach ($data as $entityData) {
             $entity = $this->entity->deserialize($entityData);
             $entity->setApi($this->api);
             $this->items[] = $entity;
         }
         $numberResults += sizeof($data);
         if (!$this->entity->isPaginated()) {
             break;
         }
         // end of results reached?
         if (sizeof($data) < $pageSize) {
             break;
         }
         if ($this->getLimit() && $numberResults >= $this->getLimit()) {
             break;
         }
         ++$params['page'];
     }
 }
Beispiel #2
0
 protected static function flushSandboxEntities()
 {
     $api = new Oauth2Api($clientId = getenv('API_CLIENT_ID'), $clientSecret = getenv('API_CLIENT_SECRET'), $refreshToken = getenv('API_REFRESH_TOKEN'), $sandbox = true);
     if (!$api->isSandbox()) {
         throw new Exception('Attempting to use this test suite on a live account will delete everything');
     }
     $api->connect();
     /** @var string[] $entities */
     $entities = ['invoice', 'project', 'contact', 'bankAccount'];
     /** @var string $entityName */
     foreach ($entities as $entityName) {
         /** @var AbstractEntity $entity */
         foreach ($api->{$entityName}()->query() as $entity) {
             if ($entity instanceof Invoice && !$entity->isDraft()) {
                 $entity->markAsDraft();
             }
             $entity->delete();
         }
     }
 }