SimpleCacheSessionHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Nofw\Session;
4
5
use Psr\Log\LoggerAwareInterface;
6
use Psr\Log\LoggerAwareTrait;
7
use Psr\Log\LoggerInterface;
8
use Psr\Log\NullLogger;
9
use Psr\SimpleCache\CacheInterface;
10
use Psr\SimpleCache\InvalidArgumentException;
11
12
/**
13
 * PSR-16 Session handler implementation.
14
 *
15
 * @author Márk Sági-Kazár <[email protected]>
16
 */
17
final class SimpleCacheSessionHandler implements \SessionHandlerInterface, LoggerAwareInterface
18
{
19
    use LoggerAwareTrait;
20
21
    /**
22
     * @var CacheInterface
23
     */
24
    private $cache;
25
26
    /**
27
     * @param CacheInterface       $cache
28
     * @param LoggerInterface|null $logger
29
     */
30 13
    public function __construct(CacheInterface $cache, LoggerInterface $logger = null)
31
    {
32 13
        $this->cache = $cache;
33 13
        $this->logger = $logger ?? new NullLogger();
34 13
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 1
    public function close()
40
    {
41 1
        return true;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 3 View Code Duplication
    public function destroy($session_id)
48
    {
49
        try {
50 3
            return $this->cache->delete($session_id);
51 1
        } catch (InvalidArgumentException $e) {
52 1
            $this->logger->error(
53 1
                $e->getMessage(),
54
                [
55 1
                    'exception' => $e,
56 1
                    'session_id' => is_scalar($session_id) ? $session_id : '** Non scalar session ID **',
57 1
                    'operation' => 'destroy',
58
                ]
59
            );
60
61 1
            return false;
62
        }
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 1
    public function gc($maxlifetime)
69
    {
70 1
        return true;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 1
    public function open($save_path, $name)
77
    {
78 1
        return true;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 2
    public function read($session_id)
85
    {
86
        try {
87 2
            return $this->cache->get($session_id);
88 1
        } catch (InvalidArgumentException $e) {
89 1
            $this->logger->error(
90 1
                $e->getMessage(),
0 ignored issues
show
Bug introduced by
The method getMessage() does not seem to exist on object<Psr\SimpleCache\InvalidArgumentException>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
                [
92 1
                    'exception' => $e,
93 1
                    'session_id' => is_scalar($session_id) ? $session_id : '** Non scalar session ID **',
94 1
                    'operation' => 'read',
95
                ]
96
            );
97
98 1
            return '';
99
        }
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 3 View Code Duplication
    public function write($session_id, $session_data)
106
    {
107
        try {
108 3
            return $this->cache->set($session_id, $session_data);
109 1
        } catch (InvalidArgumentException $e) {
110 1
            $this->logger->error(
111 1
                $e->getMessage(),
0 ignored issues
show
Bug introduced by
The method getMessage() does not seem to exist on object<Psr\SimpleCache\InvalidArgumentException>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
112
                [
113 1
                    'exception' => $e,
114 1
                    'session_id' => is_scalar($session_id) ? $session_id : '** Non scalar session ID **',
115 1
                    'operation' => 'write',
116
                ]
117
            );
118
119 1
            return false;
120
        }
121
    }
122
}
123