-
-
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.
feature #15028 Cover viewing product associations via API (TheMilek)
This PR was merged into the 1.13 branch. Discussion ---------- | Q | A | |-----------------|--------------------------------------------------------------| | Branch? | 1.13 <!-- see the comment below --> | | Bug fix? | no | | New feature? | slighly | | BC breaks? | no | | Deprecations? | no <!-- don't forget to update the UPGRADE-*.md file --> | | License | MIT | <!-- - Bug fixes must be submitted against the 1.12 branch - Features and deprecations must be submitted against the 1.13 branch - Make sure that the correct base branch is set To be sure you are not breaking any Backward Compatibilities, check the documentation: https://docs.sylius.com/en/latest/book/organization/backward-compatibility-promise.html --> Until now, we were able to see all associated products for a given product, including those that were disabled. With the new implementation, only enabled products are displayed when accesing `ProductAssociation` endpoint Commits ------- c9bff1c Cover viewing product associations via API
- Loading branch information
Showing
5 changed files
with
221 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
61 changes: 61 additions & 0 deletions
61
...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,61 @@ | ||
<?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\Core\Model\AdminUserInterface; | ||
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 instanceof AdminUserInterface && 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
104 changes: 104 additions & 0 deletions
104
.../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,104 @@ | ||
<?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\AdminUserInterface; | ||
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, | ||
AdminUserInterface $adminUser, | ||
QueryBuilder $queryBuilder, | ||
QueryNameGeneratorInterface $queryNameGenerator, | ||
): void { | ||
$userContext->getUser()->willReturn($adminUser); | ||
$adminUser->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, | ||
], | ||
); | ||
} | ||
} |