CookieStore::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Cart\Storage;
4
5
class CookieStore implements Store
6
{
7
    /**
8
     * {@inheritdoc}
9
     */
10
    public function get($cartId)
0 ignored issues
show
Coding Style introduced by
get 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...
11
    {
12
        return isset($_COOKIE[$cartId]) ? $this->decode($_COOKIE[$cartId]) : serialize([]);
13
    }
14
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function put($cartId, $data)
19
    {
20
        $this->setCookie($cartId, $this->encode($data));
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function flush($cartId)
27
    {
28
        $this->unsetCookie($cartId);
29
    }
30
31
    /**
32
     * Encode data to be saved in cookie.
33
     *
34
     * @param string $data
35
     *
36
     * @return string
37
     */
38 1
    public function encode($data)
39
    {
40 1
        return base64_encode($data);
41
    }
42
43
    /**
44
     * Decode data that has been saved in a cookie.
45
     *
46
     * @param string $data
47
     *
48
     * @return string
49
     */
50 1
    public function decode($data)
51
    {
52 1
        return base64_decode($data);
53
    }
54
55
    /**
56
     * Set cookie.
57
     *
58
     * @param string $name
59
     * @param string $data
60
     */
61
    private function setCookie($name, $data)
62
    {
63
        setcookie($name, $data);
64
    }
65
66
    /**
67
     * Unset cookie.
68
     *
69
     * @param string $name
70
     */
71
    private function unsetCookie($name)
0 ignored issues
show
Coding Style introduced by
unsetCookie 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...
72
    {
73
        unset($_COOKIE[$name]);
74
    }
75
}
76