Completed
Push — master ( 5bd0a6...5cfde8 )
by Kamil
20:21
created

FakeChannelContext   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 69
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getChannel() 0 10 1
A getMasterRequest() 0 9 2
A assertChannelWasFound() 0 6 2
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