/**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var GetContext $context */
     if ($context->hasResult()) {
         // data already retrieved
         return;
     }
     $query = $context->getQuery();
     if (!$query instanceof QueryBuilder) {
         // unsupported query
         return;
     }
     $config = $context->getConfig();
     if (null === $config) {
         // an entity configuration does not exist
         return;
     }
     $result = $this->entitySerializer->serialize($query, $config);
     if (empty($result)) {
         $result = null;
     } elseif (count($result) === 1) {
         $result = reset($result);
     } else {
         throw new \RuntimeException('The result must have one or zero items.');
     }
     $context->setResult($result);
     // data returned by the EntitySerializer are already normalized
     $context->skipGroup('normalize_data');
 }
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var GetListContext $context */
     if ($context->getResponseHeaders()->has(self::HEADER_NAME)) {
         // total count header is already set
         return;
     }
     $xInclude = $context->getRequestHeaders()->get(Context::INCLUDE_HEADER);
     if (empty($xInclude) || !in_array(self::HEADER_VALUE, $xInclude, true)) {
         // total count is not requested
         return;
     }
     $totalCount = null;
     $totalCountCallback = $context->getTotalCountCallback();
     if (null !== $totalCountCallback) {
         $totalCount = $this->executeTotalCountCallback($totalCountCallback);
     }
     $query = $context->getQuery();
     if (null !== $query && null === $totalCount) {
         $totalCount = $this->calculateTotalCount($query);
     }
     if (null !== $totalCount) {
         $context->getResponseHeaders()->set(self::HEADER_NAME, $totalCount);
     }
 }
Exemple #3
0
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var GetListContext $context */
     if (!$context->hasResult()) {
         $query = $context->getQuery();
         if (null === $query) {
             throw new NotFoundHttpException('Unsupported request.');
         } else {
             throw new NotFoundHttpException(sprintf('Unsupported query type: %s.', is_object($query) ? get_class($query) : gettype($query)));
         }
     } elseif (!is_array($context->getResult())) {
         throw new NotFoundHttpException('Getting a list of entities failed.');
     }
 }
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var GetListContext $context */
     if ($context->hasResult()) {
         // result data are already retrieved
         return;
     }
     $query = $context->getQuery();
     if ($query instanceof QueryBuilder) {
         $context->setResult($query->getQuery()->getResult());
     } elseif ($query instanceof Query) {
         $context->setResult($query->getResult());
     }
 }
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var GetListContext $context */
     if ($context->hasResult()) {
         // result data are already retrieved
         return;
     }
     $query = $context->getQuery();
     if (!$query instanceof SearchQuery) {
         // unsupported query
         return;
     }
     $searchResult = $this->searchIndex->query($query);
     $context->setResult($searchResult->toArray());
     // set callback to be used to calculate total count
     $context->setTotalCountCallback(function () use($searchResult) {
         return $searchResult->getRecordsCount();
     });
 }
 /**
  * {@inheritdoc}
  */
 public function process(ContextInterface $context)
 {
     /** @var GetListContext $context */
     if ($context->hasResult()) {
         // data already retrieved
         return;
     }
     $query = $context->getQuery();
     if (!$query instanceof QueryBuilder) {
         // unsupported query
         return;
     }
     $config = $context->getConfig();
     if (null === $config) {
         // an entity configuration does not exist
         return;
     }
     $context->setResult($this->entitySerializer->serialize($query, $config));
     // data returned by the EntitySerializer are already normalized
     $context->skipGroup('normalize_data');
 }