-
-
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.
Merge pull request #4067 from pamil/channel-cached-contexts
[Channel] Cached per request channel context
- Loading branch information
Showing
4 changed files
with
180 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
66 changes: 66 additions & 0 deletions
66
src/Sylius/Component/Channel/Context/CachedPerRequestChannelContext.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,66 @@ | ||
<?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. | ||
*/ | ||
|
||
namespace Sylius\Component\Channel\Context; | ||
|
||
use Sylius\Component\Channel\Model\ChannelInterface; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
/** | ||
* @author Kamil Kokot <kamil.kokot@lakion.com> | ||
*/ | ||
final class CachedPerRequestChannelContext implements ChannelContextInterface | ||
{ | ||
/** | ||
* @var ChannelContextInterface | ||
*/ | ||
private $decoratedChannelContext; | ||
|
||
/** | ||
* @var RequestStack | ||
*/ | ||
private $requestStack; | ||
|
||
/** | ||
* @var \SplObjectStorage|ChannelInterface[] | ||
*/ | ||
private $requestToChannelMap; | ||
|
||
/** | ||
* @param ChannelContextInterface $decoratedChannelContext | ||
* @param RequestStack $requestStack | ||
*/ | ||
public function __construct(ChannelContextInterface $decoratedChannelContext, RequestStack $requestStack) | ||
{ | ||
$this->decoratedChannelContext = $decoratedChannelContext; | ||
$this->requestStack = $requestStack; | ||
|
||
$this->requestToChannelMap = new \SplObjectStorage(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getChannel() | ||
{ | ||
$objectIdentifier = $this->requestStack->getMasterRequest(); | ||
|
||
if (null === $objectIdentifier) { | ||
return $this->decoratedChannelContext->getChannel(); | ||
} | ||
|
||
if (!isset($this->requestToChannelMap[$objectIdentifier])) { | ||
$this->requestToChannelMap[$objectIdentifier] = $this->decoratedChannelContext->getChannel(); | ||
} | ||
|
||
return $this->requestToChannelMap[$objectIdentifier]; | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
src/Sylius/Component/Channel/spec/Context/CachedPerRequestChannelContextSpec.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. | ||
*/ | ||
|
||
namespace spec\Sylius\Component\Channel\Context; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Component\Channel\Context\CachedPerRequestChannelContext; | ||
use Sylius\Component\Channel\Context\ChannelContextInterface; | ||
use Sylius\Component\Channel\Model\ChannelInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
/** | ||
* @mixin CachedPerRequestChannelContext | ||
* | ||
* @author Kamil Kokot <kamil.kokot@lakion.com> | ||
*/ | ||
class CachedPerRequestChannelContextSpec extends ObjectBehavior | ||
{ | ||
function let(ChannelContextInterface $decoratedChannelContext, RequestStack $requestStack) | ||
{ | ||
$this->beConstructedWith($decoratedChannelContext, $requestStack); | ||
} | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Sylius\Component\Channel\Context\CachedPerRequestChannelContext'); | ||
} | ||
|
||
function it_implements_channel_context_interface() | ||
{ | ||
$this->shouldImplement(ChannelContextInterface::class); | ||
} | ||
|
||
function it_caches_channels_for_the_same_request( | ||
ChannelContextInterface $decoratedChannelContext, | ||
RequestStack $requestStack, | ||
Request $request, | ||
ChannelInterface $channel | ||
) { | ||
$requestStack->getMasterRequest()->willReturn($request, $request); | ||
|
||
$decoratedChannelContext->getChannel()->willReturn($channel)->shouldBeCalledTimes(1); | ||
|
||
$this->getChannel()->shouldReturn($channel); | ||
$this->getChannel()->shouldReturn($channel); | ||
} | ||
|
||
function it_does_not_cache_channels_for_different_requests( | ||
ChannelContextInterface $decoratedChannelContext, | ||
RequestStack $requestStack, | ||
Request $firstRequest, | ||
Request $secondRequest, | ||
ChannelInterface $firstChannel, | ||
ChannelInterface $secondChannel | ||
) { | ||
$requestStack->getMasterRequest()->willReturn($firstRequest, $secondRequest); | ||
|
||
$decoratedChannelContext->getChannel()->willReturn($firstChannel, $secondChannel); | ||
|
||
$this->getChannel()->shouldReturn($firstChannel); | ||
$this->getChannel()->shouldReturn($secondChannel); | ||
} | ||
|
||
function it_caches_channels_for_the_same_request_even_if_there_are_other_request_in_between( | ||
ChannelContextInterface $decoratedChannelContext, | ||
RequestStack $requestStack, | ||
Request $firstRequest, | ||
Request $secondRequest, | ||
ChannelInterface $firstChannel, | ||
ChannelInterface $secondChannel | ||
) { | ||
$requestStack->getMasterRequest()->willReturn($firstRequest, $secondRequest, $firstRequest); | ||
|
||
$decoratedChannelContext->getChannel()->willReturn($firstChannel, $secondChannel)->shouldBeCalledTimes(2); | ||
|
||
$this->getChannel()->shouldReturn($firstChannel); | ||
$this->getChannel()->shouldReturn($secondChannel); | ||
$this->getChannel()->shouldReturn($firstChannel); | ||
} | ||
|
||
function it_does_not_cache_results_while_there_are_no_master_requests( | ||
ChannelContextInterface $decoratedChannelContext, | ||
RequestStack $requestStack, | ||
ChannelInterface $firstChannel, | ||
ChannelInterface $secondChannel | ||
) { | ||
$requestStack->getMasterRequest()->willReturn(null, null); | ||
|
||
$decoratedChannelContext->getChannel()->willReturn($firstChannel, $secondChannel)->shouldBeCalledTimes(2); | ||
|
||
$this->getChannel()->shouldReturn($firstChannel); | ||
$this->getChannel()->shouldReturn($secondChannel); | ||
} | ||
} |