public function getAll(Client $client, QueryAllRequestInterface $request)
 {
     $lastId = null;
     $data = ['results' => []];
     do {
         $request->sort('id')->limit(static::DEFAULT_PAGE_SIZE)->withTotal(false);
         if ($lastId != null) {
             $request->where('id > "' . $lastId . '"');
         }
         $response = $client->execute($request);
         if ($response->isError() || is_null($response->toObject())) {
             break;
         }
         $results = $response->toArray()['results'];
         $data['results'] = array_merge($data['results'], $results);
         $lastId = end($results)['id'];
     } while (count($results) >= static::DEFAULT_PAGE_SIZE);
     $result = $request->mapResult($data, $client->getConfig()->getContext());
     return $result;
 }
 /**
  * @param $repository
  * @param $cacheKey
  * @param AbstractApiRequest $request
  * @param int $ttl
  * @return \Commercetools\Core\Model\Common\JsonDeserializeInterface|null
  */
 protected function retrieve($repository, $cacheKey, AbstractApiRequest $request, $force = false, $ttl = self::CACHE_TTL)
 {
     if (!$force && $this->config['cache.' . $repository] && $this->cache->has($cacheKey)) {
         $cachedData = $this->cache->fetch($cacheKey);
         if (empty($cachedData)) {
             throw new NotFoundHttpException("resource not found");
         }
         $result = unserialize($cachedData);
         $result->setContext($this->client->getConfig()->getContext());
     } else {
         $this->profiler->enter($profile = new Profile('retrieve' . ucfirst($repository)));
         $response = $request->executeWithClient($this->client);
         $this->profiler->leave($profile);
         if ($response->isError() || is_null($response->toObject())) {
             $this->store($repository, $cacheKey, '', $ttl);
             throw new NotFoundHttpException("resource not found");
         }
         $result = $request->mapResponse($response);
         $this->store($repository, $cacheKey, serialize($result), $ttl);
     }
     return $result;
 }
 /**
  * @param string $locale
  * @param Context $context
  * @param Config $config
  * @return Client
  */
 public function build($locale = null, Context $context = null, Config $config = null)
 {
     if (is_null($config)) {
         $config = $this->config;
     }
     if (is_null($context)) {
         $context = $this->contextFactory->build($locale);
     }
     $config->setContext($context);
     if (is_null($this->logger)) {
         $client = Client::ofConfigAndCache($config, $this->cache);
     } else {
         $client = Client::ofConfigCacheAndLogger($config, $this->cache, $this->logger);
     }
     if ($this->profiler instanceof CommercetoolsProfilerExtension) {
         $client->getHttpClient()->addHandler($this->profiler->getProfileMiddleWare());
     }
     return $client;
 }
 /**
  * @param $locale
  * @param $clientCredentials
  * @param $fallbackLanguages
  * @return static
  */
 public function build($locale, $clientCredentials = null, $fallbackLanguages = null)
 {
     if (is_null($clientCredentials)) {
         $clientCredentials = $this->clientCredentials;
     }
     if (is_null($fallbackLanguages)) {
         $fallbackLanguages = $this->fallbackLanguages;
     }
     $language = \Locale::getPrimaryLanguage($locale);
     $languages = array_merge([$language], $fallbackLanguages[$language]);
     $context = Context::of()->setLanguages($languages)->setGraceful(true)->setLocale($locale);
     if (getenv('SPHERE_CLIENT_ID')) {
         $config = ['client_id' => getenv('SPHERE_CLIENT_ID'), 'client_secret' => getenv('SPHERE_CLIENT_SECRET'), 'project' => getenv('SPHERE_PROJECT')];
     } else {
         $config = $clientCredentials;
     }
     $config = Config::fromArray($config)->setContext($context);
     if (is_null($this->logger)) {
         return Client::ofConfigAndCache($config, $this->cache);
     }
     return Client::ofConfigCacheAndLogger($config, $this->cache, $this->logger);
 }
 /**
  * @param Client $client
  * @param $cacheKey
  * @param AbstractApiRequest $request
  * @param int $ttl
  * @return \Commercetools\Core\Model\Common\JsonDeserializeInterface|null
  */
 protected function retrieve(Client $client, $cacheKey, AbstractApiRequest $request, $locale, $force = false, $ttl = self::CACHE_TTL)
 {
     if (!$force && $this->enableCache && $this->cache->hasItem($cacheKey)) {
         $cachedData = $this->cache->getItem($cacheKey);
         if (empty($cachedData)) {
             throw new NotFoundHttpException("resource not found");
         }
         $result = unserialize($cachedData->get());
         $result->setContext($client->getConfig()->getContext());
     } else {
         $response = $request->executeWithClient($client);
         if ($response->isError() || is_null($response->toObject())) {
             $this->store($cacheKey, '', $ttl);
             throw new NotFoundHttpException("resource not found");
         }
         $result = $request->mapFromResponse($response, $this->getMapper($locale));
         $this->store($cacheKey, serialize($result), $ttl);
     }
     return $result;
 }