Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add QueryResultItemExtensionInterface #737

Merged
merged 1 commit into from
Sep 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/OrderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @author Kévin Dunglas <dunglas@gmail.com>
* @author Samuel ROZE <samuel.roze@gmail.com>
*/
class OrderExtension implements QueryCollectionExtensionInterface
final class OrderExtension implements QueryCollectionExtensionInterface
{
private $order;

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
final 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);
}
15 changes: 8 additions & 7 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 All @@ -87,10 +92,6 @@ public function getItem(string $resourceClass, $id, string $operationName = null
*/
private function addWhereForIdentifiers(array $identifiers, QueryBuilder $queryBuilder)
{
if (empty($identifiers)) {
return;
}

foreach ($identifiers as $identifier => $value) {
$placeholder = ':id_'.$identifier;
$expression = $queryBuilder->expr()->eq(
Expand Down Expand Up @@ -140,10 +141,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));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this but it seems more a PropertyNotFound then an InvalidArgument. Let me know if I have to revert this.

}

$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