Skip to content

Commit

Permalink
Use channel context instead of hostname request resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
GSadee committed Jul 19, 2019
1 parent f218612 commit cb49e01
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 22 deletions.
20 changes: 10 additions & 10 deletions spec/Http/RequestBasedLocaleProviderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace spec\Sylius\ShopApiPlugin\Http;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Channel\Context\RequestBased\RequestResolverInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Channel\Context\ChannelNotFoundException;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\ShopApiPlugin\Exception\ChannelNotFoundException;
use Sylius\ShopApiPlugin\Http\RequestBasedLocaleProviderInterface;
use Sylius\ShopApiPlugin\Provider\SupportedLocaleProviderInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
Expand All @@ -16,10 +16,10 @@
final class RequestBasedLocaleProviderSpec extends ObjectBehavior
{
function let(
RequestResolverInterface $hostnameBasedRequestResolver,
ChannelContextInterface $channelContext,
SupportedLocaleProviderInterface $supportedLocaleProvider
): void {
$this->beConstructedWith($hostnameBasedRequestResolver, $supportedLocaleProvider);
$this->beConstructedWith($channelContext, $supportedLocaleProvider);
}

function it_implements_request_based_locale_provider_interface(): void
Expand All @@ -28,38 +28,38 @@ function it_implements_request_based_locale_provider_interface(): void
}

function it_provides_a_locale_code(
RequestResolverInterface $hostnameBasedRequestResolver,
ChannelContextInterface $channelContext,
SupportedLocaleProviderInterface $supportedLocaleProvider,
ChannelInterface $channel,
Request $request
): void {
$request->query = new ParameterBag(['locale' => 'fr_FR']);

$hostnameBasedRequestResolver->findChannel($request)->willReturn($channel);
$channelContext->getChannel()->willReturn($channel);
$supportedLocaleProvider->provide('fr_FR', $channel)->willReturn('fr_FR');

$this->getLocaleCode($request)->shouldReturn('fr_FR');
}

function it_provides_a_locale_code_even_if_it_is_not_defined_explicitly(
RequestResolverInterface $hostnameBasedRequestResolver,
ChannelContextInterface $channelContext,
SupportedLocaleProviderInterface $supportedLocaleProvider,
ChannelInterface $channel,
Request $request
): void {
$request->query = new ParameterBag([]);

$hostnameBasedRequestResolver->findChannel($request)->willReturn($channel);
$channelContext->getChannel()->willReturn($channel);
$supportedLocaleProvider->provide(null, $channel)->willReturn('en_US');

$this->getLocaleCode($request)->shouldReturn('en_US');
}

function it_throws_an_exception_if_channel_cannot_be_resolved(
RequestResolverInterface $hostnameBasedRequestResolver,
ChannelContextInterface $channelContext,
Request $request
): void {
$hostnameBasedRequestResolver->findChannel($request)->willReturn(null);
$channelContext->getChannel()->willThrow(ChannelNotFoundException::class);

$this
->shouldThrow(ChannelNotFoundException::class)
Expand Down
19 changes: 19 additions & 0 deletions src/DependencyInjection/Compiler/ChannelContextPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Sylius\ShopApiPlugin\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class ChannelContextPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
// TODO: Remove after next Sylius release (1.6.0)
$container->removeDefinition('sylius.listener.user_impersonated');

$container->removeDefinition('sylius.context.channel.cached');
}
}
19 changes: 8 additions & 11 deletions src/Http/RequestBasedLocaleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,33 @@

namespace Sylius\ShopApiPlugin\Http;

use Sylius\Component\Channel\Context\RequestBased\RequestResolverInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Channel\Context\ChannelNotFoundException;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\ShopApiPlugin\Exception\ChannelNotFoundException;
use Sylius\ShopApiPlugin\Provider\SupportedLocaleProviderInterface;
use Symfony\Component\HttpFoundation\Request;

final class RequestBasedLocaleProvider implements RequestBasedLocaleProviderInterface
{
/** @var RequestResolverInterface */
private $hostnameBasedRequestResolver;
/** @var ChannelContextInterface */
private $channelContext;

/** @var SupportedLocaleProviderInterface */
private $supportedLocaleProvider;

public function __construct(
RequestResolverInterface $hostnameBasedRequestResolver,
ChannelContextInterface $channelContext,
SupportedLocaleProviderInterface $supportedLocaleProvider
) {
$this->hostnameBasedRequestResolver = $hostnameBasedRequestResolver;
$this->channelContext = $channelContext;
$this->supportedLocaleProvider = $supportedLocaleProvider;
}

/** @throws ChannelNotFoundException */
public function getLocaleCode(Request $request): string
{
/** @var ChannelInterface|null $channel */
$channel = $this->hostnameBasedRequestResolver->findChannel($request);
if (null === $channel) {
throw ChannelNotFoundException::occur();
}
/** @var ChannelInterface $channel */
$channel = $this->channelContext->getChannel();

return $this->supportedLocaleProvider->provide($request->query->get('locale'), $channel);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services/http.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</service>

<service id="sylius.shop_api_plugin.http.request_based_locale_provider" class="Sylius\ShopApiPlugin\Http\RequestBasedLocaleProvider">
<argument type="service" id="sylius.context.channel.request_based.resolver.hostname_based" />
<argument type="service" id="sylius.context.channel" />
<argument type="service" id="sylius.shop_api_plugin.provider.supported_locale_provider" />
</service>
</services>
Expand Down
9 changes: 9 additions & 0 deletions src/ShopApiPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@
namespace Sylius\ShopApiPlugin;

use Sylius\Bundle\CoreBundle\Application\SyliusPluginTrait;
use Sylius\ShopApiPlugin\DependencyInjection\Compiler\ChannelContextPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

final class ShopApiPlugin extends Bundle
{
use SyliusPluginTrait;

public function build(ContainerBuilder $container): void
{
parent::build($container);

$container->addCompilerPass(new ChannelContextPass());
}
}

0 comments on commit cb49e01

Please sign in to comment.