Completed
Push — final ( 284938...ad8b8d )
by Georges
03:03
created

Driver   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A driverCheck() 0 8 2
A driverConnect() 0 4 2
A driverWrite() 0 19 4
A driverReadExpirationDate() 0 8 3
A driverDelete() 0 15 2
B driverRead() 0 18 5
A driverClear() 0 16 4
A getStats() 0 19 3
1
<?php
2
/**
3
 *
4
 * This file is part of phpFastCache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt file.
9
 *
10
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 *
13
 */
14
15
namespace phpFastCache\Drivers\Cookie;
16
17
use phpFastCache\Core\DriverAbstract;
18
use phpFastCache\Core\StandardPsr6StructureTrait;
19
use phpFastCache\Entities\driverStatistic;
20
use phpFastCache\Exceptions\phpFastCacheDriverCheckException;
21
use phpFastCache\Exceptions\phpFastCacheDriverException;
22
use Psr\Cache\CacheItemInterface;
23
24
/**
25
 * Class Driver
26
 * @package phpFastCache\Drivers
27
 */
28
class Driver extends DriverAbstract
29
{
30
    const PREFIX = 'PFC_';
31
32
    /**
33
     * Driver constructor.
34
     * @param array $config
35
     * @throws phpFastCacheDriverException
36
     */
37
    public function __construct(array $config = [])
38
    {
39
        $this->setup($config);
40
41
        if (!$this->driverCheck()) {
42
            throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
43
        }
44
    }
45
46
    /**
47
     * @return bool
48
     */
49
    public function driverCheck()
50
    {
51
        if (function_exists('setcookie')) {
52
            return true;
53
        } else {
54
            return false;
55
        }
56
    }
57
58
    /**
59
     * @return bool
60
     */
61
    protected function driverConnect()
0 ignored issues
show
Coding Style introduced by
driverConnect 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...
62
    {
63
        return !(!array_key_exists('phpFastCache', $_COOKIE) && !@setcookie('phpFastCache', 1, 10));
64
    }
65
66
    /**
67
     * @param \Psr\Cache\CacheItemInterface $item
68
     * @return mixed
69
     * @throws \InvalidArgumentException
70
     */
71
    protected function driverWrite(CacheItemInterface $item)
72
    {
73
        /**
74
         * Check for Cross-Driver type confusion
75
         */
76
        if ($item instanceof Item) {
77
            $this->driverConnect();
78
            $keyword = self::PREFIX . $item->getKey();
79
            $v = json_encode($this->driverPreWrap($item));
80
81
            if (isset($this->config[ 'limited_memory_each_object' ]) && strlen($v) > $this->config[ 'limited_memory_each_object' ]) {
82
                return false;
83
            }
84
85
            return setcookie($keyword, $v, $item->getExpirationDate()->getTimestamp(), '/');
86
        } else {
87
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
88
        }
89
    }
90
91
    /**
92
     * @param \Psr\Cache\CacheItemInterface $item
93
     * @return mixed
94
     * @throws \phpFastCache\Exceptions\phpFastCacheDriverException
95
     */
96
    protected function driverRead(CacheItemInterface $item)
0 ignored issues
show
Coding Style introduced by
driverRead 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...
97
    {
98
        $this->driverConnect();
99
        // return null if no caching
100
        // return value if in caching
101
        $keyword = self::PREFIX . $item->getKey();
102
        $x = isset($_COOKIE[ $keyword ]) ? $this->decode(json_decode($_COOKIE[ $keyword ], true)) : false;
103
104
        if ($x == false) {
105
            return null;
106
        } else {
107
            if (!is_scalar($this->driverUnwrapData($x)) && !is_null($this->driverUnwrapData($x))) {
108
                throw new phpFastCacheDriverException('Hacking attempt: The decoding returned a non-scalar value, Cookie driver does not allow this.');
109
            }
110
111
            return $x;
112
        }
113
    }
114
115
    /**
116
     * @param $key
117
     * @return int
118
     */
119
    protected function driverReadExpirationDate($key)
0 ignored issues
show
Coding Style introduced by
driverReadExpirationDate 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...
120
    {
121
        $this->driverConnect();
122
        $keyword = self::PREFIX . $key;
123
        $x = isset($_COOKIE[ $keyword ]) ? $this->decode(json_decode($_COOKIE[ $keyword ])->t) : false;
124
125
        return $x ? $x - time() : $x;
126
    }
127
128
    /**
129
     * @param \Psr\Cache\CacheItemInterface $item
130
     * @return bool
131
     * @throws \InvalidArgumentException
132
     */
133
    protected function driverDelete(CacheItemInterface $item)
0 ignored issues
show
Coding Style introduced by
driverDelete 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...
134
    {
135
        /**
136
         * Check for Cross-Driver type confusion
137
         */
138
        if ($item instanceof Item) {
139
            $this->driverConnect();
140
            $keyword = self::PREFIX . $item->getKey();
141
            $_COOKIE[ $keyword ] = null;
142
143
            return @setcookie($keyword, null, -10);
144
        } else {
145
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
146
        }
147
    }
148
149
    /**
150
     * @return bool
151
     */
152
    protected function driverClear()
0 ignored issues
show
Coding Style introduced by
driverClear 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...
153
    {
154
        $return = null;
155
        $this->driverConnect();
156
        foreach ($_COOKIE as $keyword => $value) {
157
            if (strpos($keyword, self::PREFIX) !== false) {
158
                $_COOKIE[ $keyword ] = null;
159
                $result = @setcookie($keyword, null, -10);
160
                if ($return !== false) {
161
                    $return = $result;
162
                }
163
            }
164
        }
165
166
        return $return;
167
    }
168
169
    /********************
170
     *
171
     * PSR-6 Extended Methods
172
     *
173
     *******************/
174
175
    /**
176
     * @return driverStatistic
177
     */
178
    public function getStats()
0 ignored issues
show
Coding Style introduced by
getStats 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...
179
    {
180
        $size = 0;
181
        $stat = new driverStatistic();
182
        $stat->setData($_COOKIE);
183
184
        /**
185
         * Only count PFC Cookie
186
         */
187
        foreach ($_COOKIE as $key => $value) {
188
            if (strpos($key, self::PREFIX) === 0) {
189
                $size += strlen($value);
190
            }
191
        }
192
193
        $stat->setSize($size);
194
195
        return $stat;
196
    }
197
}