-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Cover viewing product associations via API #15028
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, | ||
], | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.