Failed Conditions
Push — master ( 051ce3...5591cb )
by Florian
02:03
created

PsrCacheHandler::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Cake\Http\Session;
4
5
use Psr\Cache\CacheItemPoolInterface;
6
use SessionHandlerInterface;
7
8
/**
9
 * CacheSession provides method for saving sessions into cache engines
10
 * implementing the PSR cache interfaces
11
 *
12
 * @link https://www.php-fig.org/psr/psr-6/
13
 */
14
class PsrCacheHandler implements SessionHandlerInterface
15
{
16
    /**
17
     * PSR Cache Item Pool
18
     *
19
     * @var \Psr\Cache\CacheItemPoolInterface
20
     */
21
    protected CacheItemPoolInterface $cachePool;
22
23
    /**
24
     * @var string
25
     */
26
    protected string $cacheItemClass;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param \Psr\Cache\CacheItemPoolInterface $cacheItemPool
32
     */
33
    public function __construct(CacheItemPoolInterface $cacheItemPool)
34
    {
35
        $this->cachePool = $cacheItemPool;
36
    }
37
38
    /**
39
     * Method called on open of a database session.
40
     *
41
     * @param string $savePath The path where to store/retrieve the session.
42
     * @param string $name The session name.
43
     * @return bool Success
44
     */
45
    public function open($savePath, $name)
46
    {
47
        return true;
48
    }
49
50
    /**
51
     * Method called on close of a database session.
52
     *
53
     * @return bool Success
54
     */
55
    public function close()
56
    {
57
        return true;
58
    }
59
60
    /**
61
     * Method used to read from a cache session.
62
     *
63
     * @param string|int $id ID that uniquely identifies session in cache.
64
     * @return string Session data or empty string if it does not exist.
65
     */
66
    public function read($id)
67
    {
68
        $cacheItem = $this->cachePool->getItem($id);
69
70
        $value = $cacheItem->get();
71
72
        return $value ?? null;
73
    }
74
75
    /**
76
     * Helper function called on write for cache sessions.
77
     *
78
     * @param string|int $id ID that uniquely identifies session in cache.
79
     * @param mixed $data The data to be saved.
80
     * @return bool True for successful write, false otherwise.
81
     */
82
    public function write($id, $data)
83
    {
84
        if (!$id) {
85
            return false;
86
        }
87
88
        if ($this->cachePool->hasItem($id)) {
89
            $cacheItem = $this->cachePool->getItem($id);
90
        } else {
91
            $class = $this->cacheItemClass;
92
            $cacheItem = new $class();
93
        }
94
95
        $cacheItem->set($data);
96
97
        return true;
98
    }
99
100
    /**
101
     * Method called on the destruction of a cache session.
102
     *
103
     * @param string|int $id ID that uniquely identifies session in cache.
104
     * @return bool Always true.
105
     */
106
    public function destroy($id)
107
    {
108
        $this->cachePool->deleteItem($id);
109
110
        return true;
111
    }
112
113
    /**
114
     * Helper function called on gc for cache sessions.
115
     *
116
     * @param int $maxlifetime Sessions that have not updated for the last maxlifetime seconds will be removed.
117
     * @return bool Always true.
118
     */
119
    public function gc($maxlifetime)
120
    {
121
        return true;
122
    }
123
}
124