|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\ChannelBundle\Context\FakeChannel; |
|
13
|
|
|
|
|
14
|
|
|
use Sylius\Component\Channel\Context\ChannelContextInterface; |
|
15
|
|
|
use Sylius\Component\Channel\Context\ChannelNotFoundException; |
|
16
|
|
|
use Sylius\Component\Channel\Model\ChannelInterface; |
|
17
|
|
|
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Kamil Kokot <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
final class FakeChannelContext implements ChannelContextInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var FakeChannelCodeProviderInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $fakeChannelCodeProvider; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var ChannelRepositoryInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $channelRepository; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var RequestStack |
|
38
|
|
|
*/ |
|
39
|
|
|
private $requestStack; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param FakeChannelCodeProviderInterface $fakeChannelCodeProvider |
|
43
|
|
|
* @param ChannelRepositoryInterface $channelRepository |
|
44
|
|
|
* @param RequestStack $requestStack |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct( |
|
47
|
|
|
FakeChannelCodeProviderInterface $fakeChannelCodeProvider, |
|
48
|
|
|
ChannelRepositoryInterface $channelRepository, |
|
49
|
|
|
RequestStack $requestStack |
|
50
|
|
|
) { |
|
51
|
|
|
$this->fakeChannelCodeProvider = $fakeChannelCodeProvider; |
|
52
|
|
|
$this->channelRepository = $channelRepository; |
|
53
|
|
|
$this->requestStack = $requestStack; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getChannel() |
|
60
|
|
|
{ |
|
61
|
|
|
$fakeChannelCode = $this->fakeChannelCodeProvider->getCode($this->getMasterRequest()); |
|
62
|
|
|
|
|
63
|
|
|
$channel = $this->channelRepository->findOneByCode($fakeChannelCode); |
|
64
|
|
|
|
|
65
|
|
|
$this->assertChannelWasFound($channel); |
|
66
|
|
|
|
|
67
|
|
|
return $channel; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return Request |
|
72
|
|
|
*/ |
|
73
|
|
|
private function getMasterRequest() |
|
74
|
|
|
{ |
|
75
|
|
|
$masterRequest = $this->requestStack->getMasterRequest(); |
|
76
|
|
|
if (null === $masterRequest) { |
|
77
|
|
|
throw new ChannelNotFoundException(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $masterRequest; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param ChannelInterface|null $channel |
|
85
|
|
|
*/ |
|
86
|
|
|
private function assertChannelWasFound(ChannelInterface $channel = null) |
|
87
|
|
|
{ |
|
88
|
|
|
if (null === $channel) { |
|
89
|
|
|
throw new ChannelNotFoundException(); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|