SessionCookieJar::load()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Magister\Services\Http;
4
5
use GuzzleHttp\Cookie\CookieJar as GuzzleCookieJar;
6
use GuzzleHttp\Cookie\SetCookie;
7
use Magister\Services\Cookie\CookieJar;
8
9
/**
10
 * Class SessionCookieJar.
11
 */
12
class SessionCookieJar extends GuzzleCookieJar
13
{
14
    /**
15
     * The Magister cookie creator service.
16
     *
17
     * @var \Magister\Services\Cookie\CookieJar
18
     */
19
    protected $cookie;
20
21
    /**
22
     * Create a new session cookie jar instance.
23
     *
24
     * @param \Magister\Services\Cookie\CookieJar $cookie
25
     * @param bool                                $strict
26
     * @param array                               $cookies
27
     */
28
    public function __construct(CookieJar $cookie, $strict = false, $cookies = [])
29
    {
30
        $this->cookie = $cookie;
31
32
        $cookies = $this->load($cookies);
33
34
        parent::__construct($strict, $cookies);
35
    }
36
37
    /**
38
     * Save cookies to the client's session.
39
     *
40
     * @param \GuzzleHttp\Cookie\SetCookie $cookie
41
     *
42
     * @return bool
43
     */
44
    public function setCookie(SetCookie $cookie)
45
    {
46
        $successful = parent::setCookie($cookie);
47
48
        if ($successful) {
49
            $this->cookie->make($cookie->getName(), $cookie);
50
51
            return true;
52
        }
53
54
        return false;
55
    }
56
57
    /**
58
     * Load the contents of the client's session into the data array.
59
     *
60
     * @param array $cookies
61
     *
62
     * @return array
63
     */
64
    protected function load($cookies)
0 ignored issues
show
Coding Style introduced by
load uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
65
    {
66
        foreach ($_COOKIE as $name => $value) {
67
            $cookie = $this->cookie->get($name);
68
69
            if ($cookie instanceof SetCookie) {
70
                $cookies[] = $cookie;
71
            }
72
        }
73
74
        return $cookies;
75
    }
76
}
77