Example #1
0
 public function testValuesAccessor()
 {
     $this->assertTrue($this->context->has('testKey'));
     $this->assertEquals($this->context->get('testKey'), 'testValue');
     $this->assertEquals($this->context->get('notExistingOne', 'testDefaultValue'), 'testDefaultValue');
     $this->context->set('testSecondKey', 'testSecondValue');
     $this->assertEquals($this->context->get('testSecondKey'), 'testSecondValue');
 }
 /**
  * {@inheritdoc}
  */
 public function handle(Context $context)
 {
     $processed = [];
     $includeRequested = explode(self::DELIMITER, $context->getRequest()->headers->get(self::HEADER_INCLUDE));
     $includeRequested = array_filter(array_map('trim', $includeRequested));
     $known = array_intersect($includeRequested, array_keys($this->handlers));
     foreach ($known as $name) {
         $serviceId = $this->handlers[$name];
         $handler = $this->container->get($serviceId);
         if ($handler instanceof IncludeHandlerInterface && $handler->supports($context)) {
             $handler->handle($context);
             $processed[] = $name;
         }
     }
     $unknown = array_diff($includeRequested, $known);
     if (!empty($unknown)) {
         $context->getResponse()->headers->set(self::HEADER_UNKNOWN, implode(self::DELIMITER, $unknown));
     }
     $unsupported = array_diff($known, $processed);
     if (!empty($unsupported)) {
         $context->getResponse()->headers->set(self::HEADER_UNSUPPORTED, implode(self::DELIMITER, $unsupported));
     }
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function handle(Context $context)
 {
     if ($context->has('totalCount')) {
         $totalCount = $context->get('totalCount');
         if (!is_callable($totalCount)) {
             throw new \InvalidArgumentException(sprintf('Expected callable for totalCount, "%s" given', is_object($totalCount) ? get_class($totalCount) : gettype($totalCount)));
         }
         $totalCount = call_user_func($totalCount);
         if (!is_int($totalCount)) {
             throw new \InvalidArgumentException(sprintf('Expected integer as result of totalCount callable, "%s" given', is_object($totalCount) ? get_class($totalCount) : gettype($totalCount)));
         }
     } else {
         if ($context->has('query')) {
             $value = $context->get('query');
             if ($value instanceof QueryBuilder) {
                 $countQb = $this->countQueryBuilderOptimizer->getCountQueryBuilder($value);
                 $query = $countQb->getQuery();
             } elseif ($value instanceof Query) {
                 $query = clone $value;
                 $query->setMaxResults(null)->setFirstResult(null);
             } elseif ($value instanceof SqlQueryBuilder) {
                 $query = clone $value;
                 $query->setMaxResults(null)->setFirstResult(null);
                 $query = $query->getQuery();
             } elseif ($value instanceof SqlQuery) {
                 $query = clone $value;
                 $query->getQueryBuilder()->setMaxResults(null)->setFirstResult(null);
             } else {
                 throw new \InvalidArgumentException(sprintf('Expected instance of Doctrine\\ORM\\QueryBuilder, Doctrine\\ORM\\Query' . ', Oro\\Component\\DoctrineUtils\\ORM\\SqlQueryBuilder' . ' or Oro\\Component\\DoctrineUtils\\ORM\\SqlQuery, "%s" given', is_object($value) ? get_class($value) : gettype($value)));
             }
         } else {
             $qb = $context->getController()->getManager()->getRepository()->createQueryBuilder('e');
             $query = $qb->getQuery();
         }
         $totalCount = $this->calculateCount($query);
     }
     $context->getResponse()->headers->set(self::HEADER_NAME, $totalCount);
 }