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

[API] Refactor StateProviders #16567

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<uriVariable parameterName="tokenValue" fromClass="Sylius\Component\Core\Model\Order" fromProperty="adjustments" />
</uriVariables>
<operations>
<operation class="ApiPlatform\Metadata\GetCollection" provider="sylius_api.state_provider.order_recursive_adjustments">
<operation class="ApiPlatform\Metadata\GetCollection" provider="sylius_api.state_provider.admin.order.adjustment.collection">
<parameters>
<parameter key="type" />
</parameters>
Expand All @@ -67,7 +67,7 @@
<uriVariable parameterName="id" fromClass="Sylius\Component\Core\Model\OrderItem" fromProperty="adjustments" />
</uriVariables>
<operations>
<operation class="ApiPlatform\Metadata\GetCollection" provider="sylius_api.state_provider.order_item_recursive_adjustments">
<operation class="ApiPlatform\Metadata\GetCollection" provider="sylius_api.state_provider.admin.order_item.adjustment.collection">
<parameters>
<parameter key="type" />
</parameters>
Expand Down Expand Up @@ -127,7 +127,7 @@
<uriVariable parameterName="tokenValue" fromClass="Sylius\Component\Core\Model\Order" fromProperty="adjustments" />
</uriVariables>
<operations>
<operation class="ApiPlatform\Metadata\GetCollection" provider="sylius_api.state_provider.order_recursive_adjustments">
<operation class="ApiPlatform\Metadata\GetCollection" provider="sylius_api.state_provider.shop.order.adjustment.collection">
<parameters>
<parameter key="type" />
</parameters>
Expand All @@ -154,7 +154,7 @@
<uriVariable parameterName="tokenValue" fromClass="Sylius\Component\Core\Model\Order" fromProperty="items" />
</uriVariables>
<operations>
<operation class="ApiPlatform\Metadata\GetCollection" provider="sylius_api.state_provider.shop.order_item_adjustments">
<operation class="ApiPlatform\Metadata\GetCollection" provider="sylius_api.state_provider.shop.order.order_item.adjustment.collection">
<parameters>
<parameter key="type" />
</parameters>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
class="ApiPlatform\Metadata\Patch"
uriTemplate="/admin/administrators/reset-password/{token}"
input="Sylius\Bundle\CoreBundle\Message\Admin\Account\ResetPassword"
provider="Sylius\Bundle\ApiBundle\StateProvider\Admin\ResetPasswordProvider"
provider="sylius_api.state_provider.admin.admin_user.reset_password"
>
<denormalizationContext>
<values>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
</normalizationContext>
</operation>

<operation class="ApiPlatform\Metadata\GetCollection" provider="sylius_api.state_provider.shop.channel" uriTemplate="/shop/channels">
<operation class="ApiPlatform\Metadata\GetCollection" provider="sylius_api.state_provider.shop.channel.collection" uriTemplate="/shop/channels">
<normalizationContext>
<values>
<value name="groups">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
class="ApiPlatform\Metadata\Patch"
uriTemplate="/shop/customers/verify/{token}"
input="Sylius\Bundle\ApiBundle\Command\Account\VerifyShopUser"
provider="Sylius\Bundle\ApiBundle\StateProvider\Input\VerifyShopUserStateProvider"
provider="sylius_api.state_provider.shop.shop_user.verify_shop_user"
>
<denormalizationContext>
<values>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<uriVariable parameterName="code" fromClass="Sylius\Component\Core\Model\Product" fromProperty="attributes"/>
</uriVariables>
<operations>
<operation class="ApiPlatform\Metadata\GetCollection" provider="sylius_api.state_provider.product_attributes">
<operation class="ApiPlatform\Metadata\GetCollection" provider="sylius_api.state_provider.shop.product.product_attribute_value.collection">
<normalizationContext>
<values>
<value name="groups">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
<resource
class="Sylius\Bundle\ApiBundle\Command\Account\ResetPassword"
messenger="input"
provider="Sylius\Bundle\ApiBundle\StateProvider\ShopUserResetPasswordProvider"
shortName="ResetPassword"
>
<validationContext>
Expand Down Expand Up @@ -62,6 +61,7 @@
output="false"
status="202"
uriTemplate="/shop/customers/reset-password/{token}"
provider="sylius_api.state_provider.shop.shop_user.reset_password"
>
<denormalizationContext>
<values>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,59 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"
>
<services>
<service id="sylius_api.state_provider.order_recursive_adjustments" class="Sylius\Bundle\ApiBundle\StateProvider\RecursiveAdjustmentsProvider">
<service id="sylius_api.state_provider.admin.order.adjustment.collection" class="Sylius\Bundle\ApiBundle\StateProvider\Common\Adjustment\CollectionProvider">
<argument type="service" id="sylius.repository.order" />
<argument type="string">tokenValue</argument>
<tag name="api_platform.state_provider" />
<tag name="api_platform.state_provider" priority="10"/>
</service>

<service id="sylius_api.state_provider.shop.order.adjustment.collection" class="Sylius\Bundle\ApiBundle\StateProvider\Common\Adjustment\CollectionProvider">
<argument type="service" id="sylius.repository.order" />
<argument type="string">tokenValue</argument>
<tag name="api_platform.state_provider" priority="10"/>
</service>

<service id="sylius_api.state_provider.order_item_recursive_adjustments" class="Sylius\Bundle\ApiBundle\StateProvider\RecursiveAdjustmentsProvider">

<service id="sylius_api.state_provider.admin.order_item.adjustment.collection" class="Sylius\Bundle\ApiBundle\StateProvider\Common\Adjustment\CollectionProvider">
<argument type="service" id="sylius.repository.order_item" />
<argument type="string">id</argument>
<tag name="api_platform.state_provider" />
<tag name="api_platform.state_provider" priority="10"/>
</service>

<service id="sylius_api.state_provider.shop.order_item_adjustments" class="Sylius\Bundle\ApiBundle\StateProvider\Shop\OrderItemAdjustmentsProvider">
<service id="sylius_api.state_provider.shop.order.order_item.adjustment.collection" class="Sylius\Bundle\ApiBundle\StateProvider\Shop\Order\OrderItem\Adjustment\CollectionProvider">
<argument type="service" id="sylius.repository.order_item" />
<tag name="api_platform.state_provider" />
<argument type="service" id="Sylius\Bundle\CoreBundle\SectionResolver\SectionProviderInterface" />
<tag name="api_platform.state_provider" priority="10"/>
</service>

<service id="Sylius\Bundle\ApiBundle\StateProvider\ShopUserResetPasswordProvider">
<tag name="api_platform.state_provider" />
<service id="sylius_api.state_provider.shop.shop_user.reset_password" class="Sylius\Bundle\ApiBundle\StateProvider\Shop\ShopUser\ResetPasswordProvider">
<argument type="service" id="Sylius\Bundle\CoreBundle\SectionResolver\SectionProviderInterface" />
<tag name="api_platform.state_provider" priority="10" />
</service>

<service id="Sylius\Bundle\ApiBundle\StateProvider\Input\VerifyShopUserStateProvider">
<service id="sylius_api.state_provider.shop.shop_user.verify_shop_user" class="Sylius\Bundle\ApiBundle\StateProvider\Shop\ShopUser\VerifyShopUserProvider">
<argument type="service" id="Sylius\Bundle\CoreBundle\SectionResolver\SectionProviderInterface" />
<tag name="api_platform.state_provider" priority="10" />
</service>

<service id="Sylius\Bundle\ApiBundle\StateProvider\Admin\ResetPasswordProvider">
<service id="sylius_api.state_provider.admin.admin_user.reset_password" class="Sylius\Bundle\ApiBundle\StateProvider\Admin\AdminUser\ResetPasswordProvider">
<argument type="service" id="Sylius\Bundle\CoreBundle\SectionResolver\SectionProviderInterface" />
<tag name="api_platform.state_provider" priority="10" />
</service>

<service id="sylius_api.state_provider.shop.channel" class="Sylius\Bundle\ApiBundle\StateProvider\Shop\ChannelProvider" >
<service id="sylius_api.state_provider.shop.channel.collection" class="Sylius\Bundle\ApiBundle\StateProvider\Shop\Channel\CollectionProvider" >
<argument type="service" id="Sylius\Bundle\CoreBundle\SectionResolver\SectionProviderInterface" />
<tag name="api_platform.state_provider" priority="10" />
</service>

<service id="sylius_api.state_provider.product_attributes" class="Sylius\Bundle\ApiBundle\StateProvider\ProductAttributesProvider">
<service id="sylius_api.state_provider.shop.product.product_attribute_value.collection" class="Sylius\Bundle\ApiBundle\StateProvider\Shop\Product\ProductAttributeValue\CollectionProvider">
<argument type="tagged_iterator" tag="api_platform.doctrine.orm.query_extension.collection" />
<argument type="service" id="Sylius\Bundle\CoreBundle\SectionResolver\SectionProviderInterface" />
<argument type="service" id="sylius.repository.product_attribute_value" />
<argument type="service" id="sylius.context.locale" />
<argument type="service" id="sylius.locale_provider.channel_based" />
<argument>%locale%</argument>
<tag name="api_platform.state_provider" />
<tag name="api_platform.state_provider" priority="10"/>
</service>

<service id="sylius_api.state_provider.shop.order_item.item" class="Sylius\Bundle\ApiBundle\StateProvider\Shop\OrderItem\ItemProvider">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* 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\StateProvider\Admin\AdminUser;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\State\ProviderInterface;
use Sylius\Bundle\ApiBundle\SectionResolver\AdminApiSection;
use Sylius\Bundle\CoreBundle\Message\Admin\Account\ResetPassword;
use Sylius\Bundle\CoreBundle\SectionResolver\SectionProviderInterface;
use Webmozart\Assert\Assert;

/** @implements ProviderInterface<ResetPassword> */
final readonly class ResetPasswordProvider implements ProviderInterface
{
public function __construct(private SectionProviderInterface $sectionProvider)
{
}

public function provide(Operation $operation, array $uriVariables = [], array $context = []): ResetPassword
{
Assert::true(is_a($operation->getClass(), ResetPassword::class, true));
Assert::isInstanceOf($operation, Patch::class);
Assert::isInstanceOf($this->sectionProvider->getSection(), AdminApiSection::class);
Assert::stringNotEmpty($uriVariables['token'] ?? null, 'Token is required.');

return new ResetPassword($uriVariables['token']);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\StateProvider;
namespace Sylius\Bundle\ApiBundle\StateProvider\Common\Adjustment;

use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use Doctrine\Common\Collections\Collection;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Order\Model\RecursiveAdjustmentsAwareInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Webmozart\Assert\Assert;

final readonly class RecursiveAdjustmentsProvider implements ProviderInterface
/** @implements ProviderInterface<AdjustmentInterface> */
final readonly class CollectionProvider implements ProviderInterface
{
public function __construct(
private RepositoryInterface $repository,
Expand All @@ -37,10 +40,10 @@ public function __construct(
/** @return Collection<AdjustmentInterface> */
public function provide(Operation $operation, array $uriVariables = [], array $context = []): Collection
{
Assert::true(is_a($operation->getClass(), AdjustmentInterface::class, true));
Assert::isInstanceOf($operation, GetCollection::class);
$identifier = $uriVariables[$this->identifier] ?? null;
if (null === $identifier) {
throw new \RuntimeException('No identifier provided in `uri_variables`.');
}
Assert::notNull($identifier, 'No identifier provided in `uri_variables`.');

/** @var RecursiveAdjustmentsAwareInterface|null $adjustable */
$adjustable = $this->repository->findOneBy([$this->identifier => $identifier]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* 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\StateProvider\Shop\Channel;

use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use Sylius\Bundle\ApiBundle\SectionResolver\ShopApiSection;
use Sylius\Bundle\ApiBundle\Serializer\ContextKeys;
use Sylius\Bundle\CoreBundle\SectionResolver\SectionProviderInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Webmozart\Assert\Assert;

/** @implements ProviderInterface<ChannelInterface> */
final readonly class CollectionProvider implements ProviderInterface
{
public function __construct(private SectionProviderInterface $sectionProvider)
{
}

public function provide(Operation $operation, array $uriVariables = [], array $context = []): array
{
Assert::true(is_a($operation->getClass(), ChannelInterface::class, true));
Assert::isInstanceOf($operation, GetCollection::class);
Assert::isInstanceOf($this->sectionProvider->getSection(), ShopApiSection::class);
Assert::keyExists($context, ContextKeys::CHANNEL);

return [$context[ContextKeys::CHANNEL]];
}
}
52 changes: 0 additions & 52 deletions src/Sylius/Bundle/ApiBundle/StateProvider/Shop/ChannelProvider.php

This file was deleted.

Loading
Loading