Skip to content

Commit

Permalink
Add QueryResultItemExtensionInterface (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
abluchet committed Sep 8, 2016
1 parent 5c706e2 commit 2b61a2e
Show file tree
Hide file tree
Showing 7 changed files with 287 additions and 21 deletions.
8 changes: 3 additions & 5 deletions src/Bridge/Doctrine/Orm/CollectionDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace ApiPlatform\Core\Bridge\Doctrine\Orm;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
Expand Down Expand Up @@ -60,10 +60,8 @@ public function getCollection(string $resourceClass, string $operationName = nul
foreach ($this->collectionExtensions as $extension) {
$extension->applyToCollection($queryBuilder, $queryNameGenerator, $resourceClass, $operationName);

if ($extension instanceof QueryResultExtensionInterface) {
if ($extension->supportsResult($resourceClass, $operationName)) {
return $extension->getResult($queryBuilder);
}
if ($extension instanceof QueryResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName)) {
return $extension->getResult($queryBuilder);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/Doctrine/Orm/Extension/PaginationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @author Kévin Dunglas <dunglas@gmail.com>
* @author Samuel ROZE <samuel.roze@gmail.com>
*/
class PaginationExtension implements QueryResultExtensionInterface
class PaginationExtension implements QueryResultCollectionExtensionInterface
{
private $managerRegistry;
private $requestStack;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @author Samuel ROZE <samuel.roze@gmail.com>
* @author Kévin Dunglas <dunglas@gmail.com>
*/
interface QueryResultExtensionInterface extends QueryCollectionExtensionInterface
interface QueryResultCollectionExtensionInterface extends QueryCollectionExtensionInterface
{
/**
* @param string $resourceClass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Extension;

use Doctrine\ORM\QueryBuilder;

/**
* Interface of Doctrine ORM query extensions that supports result production
* for specific cases such as Query alteration.
*
* @author Antoine BLUCHET <soyuka@gmail.com>
*/
interface QueryResultItemExtensionInterface extends QueryItemExtensionInterface
{
/**
* @param string $resourceClass
* @param string|null $operationName
*
* @return bool
*/
public function supportsResult(string $resourceClass, string $operationName = null) : bool;

/**
* @param QueryBuilder $queryBuilder
*
* @return mixed
*/
public function getResult(QueryBuilder $queryBuilder);
}
11 changes: 8 additions & 3 deletions src/Bridge/Doctrine/Orm/ItemDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
namespace ApiPlatform\Core\Bridge\Doctrine\Orm;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultItemExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use ApiPlatform\Core\Exception\PropertyNotFoundException;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
Expand Down Expand Up @@ -74,6 +75,10 @@ public function getItem(string $resourceClass, $id, string $operationName = null

foreach ($this->itemExtensions as $extension) {
$extension->applyToItem($queryBuilder, $queryNameGenerator, $resourceClass, $identifiers, $operationName);

if ($extension instanceof QueryResultItemExtensionInterface && $extension->supportsResult($resourceClass, $operationName)) {
return $extension->getResult($queryBuilder);
}
}

return $queryBuilder->getQuery()->getOneOrNullResult();
Expand Down Expand Up @@ -140,10 +145,10 @@ private function normalizeIdentifiers($id, ObjectManager $manager, string $resou
continue;
}

$identifier = $identifiersMap[$propertyName] ?? $identifierValues[$i] ?? null;
$identifier = !isset($identifiersMap) ? $identifierValues[$i] ?? null : $identifiersMap[$propertyName] ?? null;

if (null === $identifier) {
throw new InvalidArgumentException(sprintf('Invalid identifier "%s".', $id));
throw new PropertyNotFoundException(sprintf('Invalid identifier "%s", "%s" has not been found.', $id, $propertyName));
}

$identifiers[$propertyName] = $identifier;
Expand Down
22 changes: 11 additions & 11 deletions tests/Bridge/Doctrine/Orm/CollectionDataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use ApiPlatform\Core\Bridge\Doctrine\Orm\CollectionDataProvider;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
use Doctrine\Common\Persistence\ManagerRegistry;
Expand Down Expand Up @@ -47,10 +47,10 @@ public function testGetCollection()
$managerRegistryProphecy = $this->prophesize(ManagerRegistry::class);
$managerRegistryProphecy->getManagerForClass(Dummy::class)->willReturn($managerProphecy->reveal())->shouldBeCalled();

$exstensionProphecy = $this->prophesize(QueryCollectionExtensionInterface::class);
$exstensionProphecy->applyToCollection($queryBuilder, Argument::type(QueryNameGeneratorInterface::class), Dummy::class, 'foo')->shouldBeCalled();
$extensionProphecy = $this->prophesize(QueryCollectionExtensionInterface::class);
$extensionProphecy->applyToCollection($queryBuilder, Argument::type(QueryNameGeneratorInterface::class), Dummy::class, 'foo')->shouldBeCalled();

$dataProvider = new CollectionDataProvider($managerRegistryProphecy->reveal(), [$exstensionProphecy->reveal()]);
$dataProvider = new CollectionDataProvider($managerRegistryProphecy->reveal(), [$extensionProphecy->reveal()]);
$this->assertEquals([], $dataProvider->getCollection(Dummy::class, 'foo'));
}

Expand All @@ -68,12 +68,12 @@ public function testQueryResultExtension()
$managerRegistryProphecy = $this->prophesize(ManagerRegistry::class);
$managerRegistryProphecy->getManagerForClass(Dummy::class)->willReturn($managerProphecy->reveal())->shouldBeCalled();

$exstensionProphecy = $this->prophesize(QueryResultExtensionInterface::class);
$exstensionProphecy->applyToCollection($queryBuilder, Argument::type(QueryNameGeneratorInterface::class), Dummy::class, 'foo')->shouldBeCalled();
$exstensionProphecy->supportsResult(Dummy::class, 'foo')->willReturn(true)->shouldBeCalled();
$exstensionProphecy->getResult($queryBuilder)->willReturn([])->shouldBeCalled();
$extensionProphecy = $this->prophesize(QueryResultCollectionExtensionInterface::class);
$extensionProphecy->applyToCollection($queryBuilder, Argument::type(QueryNameGeneratorInterface::class), Dummy::class, 'foo')->shouldBeCalled();
$extensionProphecy->supportsResult(Dummy::class, 'foo')->willReturn(true)->shouldBeCalled();
$extensionProphecy->getResult($queryBuilder)->willReturn([])->shouldBeCalled();

$dataProvider = new CollectionDataProvider($managerRegistryProphecy->reveal(), [$exstensionProphecy->reveal()]);
$dataProvider = new CollectionDataProvider($managerRegistryProphecy->reveal(), [$extensionProphecy->reveal()]);
$this->assertEquals([], $dataProvider->getCollection(Dummy::class, 'foo'));
}

Expand Down Expand Up @@ -103,9 +103,9 @@ public function testThrowResourceClassNotSupportedException()
$managerRegistryProphecy = $this->prophesize(ManagerRegistry::class);
$managerRegistryProphecy->getManagerForClass(Dummy::class)->willReturn(null)->shouldBeCalled();

$extenstensionProphecy = $this->prophesize(QueryResultExtensionInterface::class);
$extensionProphecy = $this->prophesize(QueryResultCollectionExtensionInterface::class);

$dataProvider = new CollectionDataProvider($managerRegistryProphecy->reveal(), [$extenstensionProphecy->reveal()]);
$dataProvider = new CollectionDataProvider($managerRegistryProphecy->reveal(), [$extensionProphecy->reveal()]);
$dataProvider->getCollection(Dummy::class, 'foo');
}
}
Loading

0 comments on commit 2b61a2e

Please sign in to comment.