Completed
Push — master ( 91eee7...fe719d )
by Márk
13s
created

CacheSessionHandler::destroy()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 17
loc 17
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 10
nc 2
nop 1
crap 3
1
<?php
2
3
namespace Nofw\Session;
4
5
use Psr\Cache\CacheItemPoolInterface;
6
use Psr\Cache\InvalidArgumentException;
7
use Psr\Log\LoggerAwareInterface;
8
use Psr\Log\LoggerAwareTrait;
9
use Psr\Log\LoggerInterface;
10
use Psr\Log\NullLogger;
11
12
/**
13
 * PSR-6 Session handler implementation.
14
 *
15
 * @author Márk Sági-Kazár <[email protected]>
16
 */
17
final class CacheSessionHandler implements \SessionHandlerInterface, LoggerAwareInterface
18
{
19
    use LoggerAwareTrait;
20
21
    /**
22
     * @var CacheItemPoolInterface
23
     */
24
    private $pool;
25
26
    /**
27
     * @param CacheItemPoolInterface $pool
28
     * @param LoggerInterface|null   $logger
29
     */
30 14
    public function __construct(CacheItemPoolInterface $pool, LoggerInterface $logger = null)
31
    {
32 14
        $this->pool = $pool;
33 14
        $this->logger = $logger ?? new NullLogger();
34 14
    }
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)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
    {
49
        try {
50 3
            return $this->pool->deleteItem($session_id);
51 1
        } catch (InvalidArgumentException $e) {
52 1
            $this->logger->error(
53 1
                $e->getMessage(),
1 ignored issue
show
Bug introduced by
The method getMessage() does not seem to exist on object<Psr\Cache\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...
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 3
    public function read($session_id)
85
    {
86
        try {
87 3
            $item = $this->pool->getItem($session_id);
88
89 2
            if ($item->isHit()) {
90 1
                return $item->get();
91
            }
92
93 1
            $this->logger->debug('Session not found in the storage', ['sessionId' => $session_id]);
94
95 1
            return '';
96 1
        } catch (InvalidArgumentException $e) {
97 1
            $this->logger->error(
98 1
                $e->getMessage(),
1 ignored issue
show
Bug introduced by
The method getMessage() does not seem to exist on object<Psr\Cache\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...
99
                [
100 1
                    'exception' => $e,
101 1
                    'session_id' => is_scalar($session_id) ? $session_id : '** Non scalar session ID **',
102 1
                    'operation' => 'read',
103
                ]
104
            );
105
106 1
            return '';
107
        }
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 3
    public function write($session_id, $session_data)
114
    {
115
        try {
116 3
            $item = $this->pool->getItem($session_id);
117
118 2
            $item->set($session_data);
119
120 2
            return $this->pool->save($item);
121 1
        } catch (InvalidArgumentException $e) {
122 1
            $this->logger->error(
123 1
                $e->getMessage(),
1 ignored issue
show
Bug introduced by
The method getMessage() does not seem to exist on object<Psr\Cache\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...
124
                [
125 1
                    'exception' => $e,
126 1
                    'session_id' => is_scalar($session_id) ? $session_id : '** Non scalar session ID **',
127 1
                    'operation' => 'write',
128
                ]
129
            );
130
131 1
            return false;
132
        }
133
    }
134
}
135