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
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Sylius\Bundle\ChannelBundle\Collector; |
15
|
|
|
|
16
|
|
|
use Sylius\Component\Channel\Context\ChannelContextInterface; |
17
|
|
|
use Sylius\Component\Channel\Context\ChannelNotFoundException; |
18
|
|
|
use Sylius\Component\Channel\Model\ChannelInterface; |
19
|
|
|
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\HttpFoundation\Response; |
22
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
23
|
|
|
|
24
|
|
|
final class ChannelCollector extends DataCollector |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var ChannelContextInterface |
28
|
|
|
*/ |
29
|
|
|
private $channelContext; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param ChannelRepositoryInterface $channelRepository |
33
|
|
|
* @param ChannelContextInterface $channelContext |
34
|
|
|
* @param bool $channelChangeSupport |
35
|
|
|
*/ |
36
|
|
|
public function __construct( |
37
|
|
|
ChannelRepositoryInterface $channelRepository, |
38
|
|
|
ChannelContextInterface $channelContext, |
39
|
|
|
bool $channelChangeSupport = false |
40
|
|
|
) { |
41
|
|
|
$this->channelContext = $channelContext; |
42
|
|
|
|
43
|
|
|
$this->data = [ |
44
|
|
|
'channel' => null, |
45
|
|
|
'channels' => $channelRepository->findAll(), |
46
|
|
|
'channel_change_support' => $channelChangeSupport, |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return ChannelInterface|null |
52
|
|
|
*/ |
53
|
|
|
public function getChannel(): ?ChannelInterface |
54
|
|
|
{ |
55
|
|
|
return $this->data['channel']; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return iterable|ChannelInterface[] |
60
|
|
|
*/ |
61
|
|
|
public function getChannels(): iterable |
62
|
|
|
{ |
63
|
|
|
return $this->data['channels']; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function isChannelChangeSupported(): bool |
70
|
|
|
{ |
71
|
|
|
return $this->data['channel_change_support']; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function collect(Request $request, Response $response, \Exception $exception = null): void |
78
|
|
|
{ |
79
|
|
|
try { |
80
|
|
|
$this->data['channel'] = $this->channelContext->getChannel(); |
81
|
|
|
} catch (ChannelNotFoundException $exception) { |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function reset(): void |
89
|
|
|
{ |
90
|
|
|
$this->data['channel'] = null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function getName(): string |
97
|
|
|
{ |
98
|
|
|
return 'sylius.channel_collector'; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|