Completed
Push — 1.4 ( 02a9e9...85ae77 )
by Kamil
05:53
created

ChannelCollector::pluckChannel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
    /** @var ChannelContextInterface */
27
    private $channelContext;
28
29
    public function __construct(
30
        ChannelRepositoryInterface $channelRepository,
31
        ChannelContextInterface $channelContext,
32
        bool $channelChangeSupport = false
33
    ) {
34
        $this->channelContext = $channelContext;
35
36
        $this->data = [
37
            'channel' => null,
38
            'channels' => array_map([$this, 'pluckChannel'], $channelRepository->findAll()),
39
            'channel_change_support' => $channelChangeSupport,
40
        ];
41
    }
42
43
    public function getChannel(): ?array
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
44
    {
45
        return $this->data['channel'];
46
    }
47
48
    /**
49
     * @return iterable|ChannelInterface[]
50
     */
51
    public function getChannels(): iterable
52
    {
53
        return $this->data['channels'];
54
    }
55
56
    public function isChannelChangeSupported(): bool
57
    {
58
        return $this->data['channel_change_support'];
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function collect(Request $request, Response $response, \Exception $exception = null): void
65
    {
66
        try {
67
            $this->data['channel'] = $this->pluckChannel($this->channelContext->getChannel());
68
        } catch (ChannelNotFoundException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
69
        }
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function reset(): void
76
    {
77
        $this->data['channel'] = null;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getName(): string
84
    {
85
        return 'sylius.channel_collector';
86
    }
87
88
    private function pluckChannel(ChannelInterface $channel): array
89
    {
90
        return [
91
            'name' => $channel->getName(),
92
            'hostname' => $channel->getHostname(),
93
            'code' => $channel->getCode(),
94
        ];
95
    }
96
}
97