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

ChannelContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 9
nc 1
nop 4
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\Behat\Context\Setup;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Component\Channel\Factory\ChannelFactoryInterface;
16
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
17
use Sylius\Component\Core\Test\Services\DefaultStoreDataInterface;
18
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
19
20
/**
21
 * @author Arkadiusz Krakowiak <[email protected]>
22
 * @author Mateusz Zalewski <[email protected]>
23
 */
24
final class ChannelContext implements Context
25
{
26
    /**
27
     * @var SharedStorageInterface
28
     */
29
    private $sharedStorage;
30
31
    /**
32
     * @var DefaultStoreDataInterface
33
     */
34
    private $defaultFranceChannelFactory;
35
36
    /**
37
     * @var ChannelFactoryInterface
38
     */
39
    private $channelFactory;
40
41
    /**
42
     * @var ChannelRepositoryInterface
43
     */
44
    private $channelRepository;
45
46
    /**
47
     * @param SharedStorageInterface $sharedStorage
48
     * @param DefaultStoreDataInterface $defaultFranceChannelFactory
49
     * @param ChannelFactoryInterface $channelFactory
50
     * @param ChannelRepositoryInterface $channelRepository
51
     */
52
    public function __construct(
53
        SharedStorageInterface $sharedStorage,
54
        DefaultStoreDataInterface $defaultFranceChannelFactory,
55
        ChannelFactoryInterface $channelFactory,
56
        ChannelRepositoryInterface $channelRepository
57
    ) {
58
        $this->sharedStorage = $sharedStorage;
59
        $this->defaultFranceChannelFactory = $defaultFranceChannelFactory;
60
        $this->channelFactory = $channelFactory;
61
        $this->channelRepository = $channelRepository;
62
    }
63
64
    /**
65
     * @Transform /^channel "([^"]+)"$/
66
     * @Transform /^"([^"]+)" channel/
67
     * @Transform :channel
68
     */
69
    public function getChannelByName($channelName)
70
    {
71
        $channel = $this->channelRepository->findOneBy(['name' => $channelName]);
72
        if (null === $channel) {
73
            throw new \InvalidArgumentException('Channel with name "'.$channelName.'" does not exist');
74
        }
75
76
        return $channel;
77
    }
78
79
    /**
80
     * @Transform /(?:this|that|the) channel/
81
     */
82
    public function getLatestChannel()
83
    {
84
        return $this->sharedStorage->getCurrentResource('channel');
85
    }
86
87
    /**
88
     * @Given the store operates on a single channel
89
     * @Given the store is operating on a single channel
90
     */
91
    public function thatStoreIsOperatingOnASingleChannel()
92
    {
93
        $defaultData = $this->defaultFranceChannelFactory->create();
94
        $this->sharedStorage->setClipboard($defaultData);
95
    }
96
97
    /**
98
     * @Given /^the store operates on (?:a|another) channel named "([^"]+)"$/
99
     */
100
    public function theStoreOperatesOnAChannelNamed($channelName)
101
    {
102
        $channel = $this->channelFactory->createNamed($channelName);
103
        $channel->setCode($channelName);
104
105
        $this->channelRepository->add($channel);
106
        $this->sharedStorage->setCurrentResource('channel', $channel);
107
    }
108
}
109