Completed
Push — 1.2-symfony4-part2 ( 920644...8ea8d6 )
by Kamil
113:19 queued 96:30
created

CookieSetter::shouldMinkSessionBePrepared()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 4
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\Behat\Service\Setter;
15
16
use Behat\Mink\Driver\Selenium2Driver;
17
use Behat\Mink\Session;
18
use FriendsOfBehat\SymfonyExtension\Driver\SymfonyDriver;
19
use Symfony\Component\BrowserKit\Cookie;
20
21
final class CookieSetter implements CookieSetterInterface
22
{
23
    /**
24
     * @var Session
25
     */
26
    private $minkSession;
27
28
    /**
29
     * @var array
30
     */
31
    private $minkParameters;
32
33
    /**
34
     * @param Session $minkSession
35
     * @param array $minkParameters
36
     */
37
    public function __construct(Session $minkSession, array $minkParameters)
38
    {
39
        $this->minkSession = $minkSession;
40
        $this->minkParameters = $minkParameters;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function setCookie($name, $value)
47
    {
48
        $this->prepareMinkSessionIfNeeded($this->minkSession);
49
50
        $driver = $this->minkSession->getDriver();
51
52
        if ($driver instanceof SymfonyDriver) {
0 ignored issues
show
Bug introduced by
The class FriendsOfBehat\SymfonyEx...on\Driver\SymfonyDriver does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
53
            $driver->getClient()->getCookieJar()->set(
54
                new Cookie($name, $value, null, null, parse_url($this->minkParameters['base_url'], PHP_URL_HOST))
55
            );
56
57
            return;
58
        }
59
60
        $this->minkSession->setCookie($name, $value);
61
    }
62
63
    private function prepareMinkSessionIfNeeded(Session $session): void
64
    {
65
        if ($this->shouldMinkSessionBePrepared($session)) {
66
            $session->visit(rtrim($this->minkParameters['base_url'], '/') . '/');
67
        }
68
    }
69
70
    private function shouldMinkSessionBePrepared(Session $session): bool
71
    {
72
        $driver = $session->getDriver();
73
74
        if ($driver instanceof SymfonyDriver) {
0 ignored issues
show
Bug introduced by
The class FriendsOfBehat\SymfonyEx...on\Driver\SymfonyDriver does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
75
            return false;
76
        }
77
78
        if ($driver instanceof Selenium2Driver && $driver->getWebDriverSession() === null) {
79
            return true;
80
        }
81
82
        if (false !== strpos($session->getCurrentUrl(), $this->minkParameters['base_url'])) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !(false !== strpo...rameters['base_url']));.
Loading history...
83
            return false;
84
        }
85
86
        return true;
87
    }
88
}
89