コード例 #1
0
 /**
  * @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;
 }
コード例 #2
0
 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;
 }
コード例 #3
0
 /**
  * @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;
 }