Completed
Push — master ( 42cf72...994ba8 )
by Michał
90:44 queued 57:55
created

SecurityContext::prepareSessionIfNeeded()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 6
nc 3
nop 0
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 Behat\Mink\Driver\Selenium2Driver;
16
use Behat\Mink\Mink;
17
use Behat\Mink\Session;
18
use Behat\MinkExtension\Context\MinkAwareContext;
19
use Sylius\Behat\Page\Shop\HomePage;
20
use Sylius\Bundle\CoreBundle\Test\Services\SecurityServiceInterface;
21
22
/**
23
 * @author Arkadiusz Krakowiak <[email protected]>
24
 */
25
final class SecurityContext implements Context, MinkAwareContext
26
{
27
    /**
28
     * @var SecurityServiceInterface
29
     */
30
    private $securityService;
31
32
    /**
33
     * @var HomePage
34
     */
35
    private $homePage;
36
37
    /**
38
     * @var Mink
39
     */
40
    private $mink;
41
42
    /**
43
     * @var array
44
     */
45
    private $minkParameters;
46
47
    /**
48
     * @param SecurityServiceInterface $securityService
49
     * @param HomePage $homePage
50
     */
51
    public function __construct(SecurityServiceInterface $securityService, HomePage $homePage)
52
    {
53
        $this->securityService = $securityService;
54
        $this->homePage = $homePage;
55
    }
56
57
    /**
58
     * @Given I am logged in as :email
59
     */
60
    public function iAmLoggedInAs($email)
61
    {
62
        $this->prepareSessionIfNeeded();
63
        $this->securityService->logIn($email, 'main', $this->mink->getSession());
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function setMink(Mink $mink)
70
    {
71
        $this->mink = $mink;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function setMinkParameters(array $parameters)
78
    {
79
        $this->minkParameters = $parameters;
80
    }
81
82
    private function prepareSessionIfNeeded()
83
    {
84
        if (!$this->getSession()->getDriver() instanceof Selenium2Driver) {
85
            return;
86
        }
87
88
        if (false !== strpos($this->getSession()->getCurrentUrl(), $this->minkParameters['base_url'])) {
89
            return;
90
        }
91
92
        $this->homePage->open();
93
    }
94
95
    /**
96
     * @param null $name
97
     *
98
     * @return Session
99
     */
100
    private function getSession($name = null)
101
    {
102
        return $this->mink->getSession($name);
103
    }
104
}
105