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

ChannelContext   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A iChangeMyCurrentChannelTo() 0 6 1
A prepareSessionIfNeeded() 0 12 3
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\Ui;
13
14
use Behat\Behat\Context\Context;
15
use Behat\Mink\Mink;
16
use Behat\Mink\Session;
17
use Behat\MinkExtension\Context\MinkAwareContext;
18
use Behat\Symfony2Extension\Driver\KernelDriver;
19
use Sylius\Component\Core\Model\ChannelInterface;
20
21
/**
22
 * @author Kamil Kokot <[email protected]>
23
 */
24
final class ChannelContext implements Context
25
{
26
    /**
27
     * @var Session
28
     */
29
    private $minkSession;
30
31
    /**
32
     * @var array
33
     */
34
    private $minkParameters;
35
36
    /**
37
     * @param Session $minkSession
38
     * @param array $minkParameters
39
     */
40
    public function __construct(Session $minkSession, array $minkParameters)
41
    {
42
        if (!isset($minkParameters['base_url'])) {
43
            $minkParameters['base_url'] = null;
44
        }
45
46
        $this->minkSession = $minkSession;
47
        $this->minkParameters = $minkParameters;
48
    }
49
50
    /**
51
     * @When I change my current channel to :channel
52
     */
53
    public function iChangeMyCurrentChannelTo(ChannelInterface $channel)
54
    {
55
        $this->prepareSessionIfNeeded();
56
57
        $this->minkSession->setCookie('_channel_code', $channel->getCode());
58
    }
59
60
    private function prepareSessionIfNeeded()
61
    {
62
        if ($this->minkSession->getDriver() instanceof KernelDriver) {
63
            return;
64
        }
65
66
        if (false !== strpos($this->minkSession->getCurrentUrl(), $this->minkParameters['base_url'])) {
67
            return;
68
        }
69
70
        $this->minkSession->visit(rtrim($this->minkParameters['base_url'], '/') . '/');
71
    }
72
}
73