-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cover viewing product associations via API
- Loading branch information
Showing
5 changed files
with
217 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...ApiBundle/Doctrine/QueryItemExtension/EnabledProductInProductAssociationItemExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ApiBundle\Doctrine\QueryItemExtension; | ||
|
||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface; | ||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; | ||
use Doctrine\ORM\QueryBuilder; | ||
use Sylius\Bundle\ApiBundle\Context\UserContextInterface; | ||
use Sylius\Bundle\ApiBundle\Serializer\ContextKeys; | ||
use Sylius\Component\Product\Model\ProductAssociationInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** @experimental */ | ||
final class EnabledProductInProductAssociationItemExtension implements QueryItemExtensionInterface | ||
{ | ||
public function __construct(private UserContextInterface $userContext) | ||
{ | ||
} | ||
|
||
public function applyToItem( | ||
QueryBuilder $queryBuilder, | ||
QueryNameGeneratorInterface $queryNameGenerator, | ||
string $resourceClass, | ||
array $identifiers, | ||
string $operationName = null, | ||
array $context = [], | ||
) { | ||
if (!is_a($resourceClass, ProductAssociationInterface::class, true)) { | ||
return; | ||
} | ||
|
||
$user = $this->userContext->getUser(); | ||
if ($user !== null && in_array('ROLE_API_ACCESS', $user->getRoles(), true)) { | ||
return; | ||
} | ||
|
||
Assert::keyExists($context, ContextKeys::CHANNEL); | ||
|
||
$rootAlias = $queryBuilder->getRootAliases()[0]; | ||
$enabled = $queryNameGenerator->generateParameterName('enabled'); | ||
$channel = $queryNameGenerator->generateParameterName('channel'); | ||
|
||
$queryBuilder->addSelect('associatedProduct'); | ||
$queryBuilder->leftJoin(sprintf('%s.associatedProducts', $rootAlias), 'associatedProduct', 'WITH', sprintf('associatedProduct.enabled = :%s', $enabled)); | ||
$queryBuilder->innerJoin('associatedProduct.channels', 'channel', 'WITH', sprintf('channel = :%s', $channel)); | ||
$queryBuilder->setParameter($enabled, true); | ||
$queryBuilder->setParameter($channel, $context[ContextKeys::CHANNEL]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
.../spec/Doctrine/QueryItemExtension/EnabledProductInProductAssociationItemExtensionSpec.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\Bundle\ApiBundle\Doctrine\QueryItemExtension; | ||
|
||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; | ||
use Doctrine\ORM\QueryBuilder; | ||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Bundle\ApiBundle\Context\UserContextInterface; | ||
use Sylius\Bundle\ApiBundle\Serializer\ContextKeys; | ||
use Sylius\Component\Core\Model\ChannelInterface; | ||
use Sylius\Component\Core\Model\ProductVariantInterface; | ||
use Sylius\Component\Product\Model\ProductAssociationInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
final class EnabledProductInProductAssociationItemExtensionSpec extends ObjectBehavior | ||
{ | ||
function let(UserContextInterface $userContext): void | ||
{ | ||
$this->beConstructedWith($userContext); | ||
} | ||
|
||
function it_does_nothing_if_current_resource_is_not_a_product_association( | ||
UserContextInterface $userContext, | ||
QueryBuilder $queryBuilder, | ||
QueryNameGeneratorInterface $queryNameGenerator, | ||
): void { | ||
$userContext->getUser()->shouldNotBeCalled(); | ||
$queryBuilder->getRootAliases()->shouldNotBeCalled(); | ||
|
||
$this->applyToItem( | ||
$queryBuilder, | ||
$queryNameGenerator, | ||
ProductVariantInterface::class, | ||
[], | ||
Request::METHOD_GET, | ||
); | ||
} | ||
|
||
function it_does_nothing_if_current_user_is_an_admin_user( | ||
UserContextInterface $userContext, | ||
UserInterface $user, | ||
QueryBuilder $queryBuilder, | ||
QueryNameGeneratorInterface $queryNameGenerator, | ||
): void { | ||
$userContext->getUser()->willReturn($user); | ||
$user->getRoles()->willReturn(['ROLE_API_ACCESS']); | ||
|
||
$queryBuilder->getRootAliases()->shouldNotBeCalled(); | ||
|
||
$this->applyToItem( | ||
$queryBuilder, | ||
$queryNameGenerator, | ||
ProductAssociationInterface::class, | ||
[], | ||
Request::METHOD_GET, | ||
); | ||
} | ||
|
||
function it_applies_conditions_for_customer( | ||
UserContextInterface $userContext, | ||
UserInterface $user, | ||
QueryBuilder $queryBuilder, | ||
QueryNameGeneratorInterface $queryNameGenerator, | ||
ChannelInterface $channel, | ||
): void { | ||
$userContext->getUser()->willReturn($user); | ||
$user->getRoles()->willReturn([]); | ||
|
||
$queryNameGenerator->generateParameterName('enabled')->shouldBeCalled()->willReturn('enabled'); | ||
$queryNameGenerator->generateParameterName('channel')->shouldBeCalled()->willReturn('channel'); | ||
$queryBuilder->getRootAliases()->willReturn(['o']); | ||
|
||
$queryBuilder->addSelect('associatedProduct')->shouldBeCalled()->willReturn($queryBuilder); | ||
$queryBuilder->leftJoin('o.associatedProducts', 'associatedProduct', 'WITH', 'associatedProduct.enabled = :enabled')->shouldBeCalled()->willReturn($queryBuilder); | ||
$queryBuilder->innerJoin('associatedProduct.channels', 'channel', 'WITH', 'channel = :channel')->shouldBeCalled()->willReturn($queryBuilder); | ||
$queryBuilder->setParameter('enabled', true)->shouldBeCalled()->willReturn($queryBuilder); | ||
$queryBuilder->setParameter('channel', $channel)->shouldBeCalled()->willReturn($queryBuilder); | ||
|
||
$this->applyToItem( | ||
$queryBuilder, | ||
$queryNameGenerator, | ||
ProductAssociationInterface::class, | ||
[], | ||
Request::METHOD_GET, | ||
[ | ||
ContextKeys::CHANNEL => $channel->getWrappedObject(), | ||
ContextKeys::HTTP_REQUEST_METHOD_TYPE => Request::METHOD_GET, | ||
], | ||
); | ||
} | ||
} |