Test Failed
Push — master ( 13c8bf...f549ef )
by Florian
11:18
created

PsrCacheHandler::write()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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