CacheSessionHandler   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 114
Duplicated Lines 51.75 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 14
c 3
b 0
f 0
lcom 1
cbo 5
dl 59
loc 114
ccs 41
cts 41
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A close() 0 4 1
A destroy() 17 17 3
A gc() 0 4 1
A open() 0 4 1
A read() 21 21 4
A write() 21 21 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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)
0 ignored issues
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(),
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 View Code Duplication
    public function read($session_id)
0 ignored issues
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...
85
    {
86
        try {
87 3
            $item = $this->pool->getItem($session_id);
88
89 2
            if ($item->isHit()) {
90 2
                return $item->get();
91
            }
92 1
        } catch (InvalidArgumentException $e) {
93 1
            $this->logger->error(
94 1
                $e->getMessage(),
95
                [
96 1
                    'exception' => $e,
97 1
                    'session_id' => is_scalar($session_id) ? $session_id : '** Non scalar session ID **',
98 1
                    'operation' => 'read',
99
                ]
100
            );
101
        }
102
103 2
        return '';
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 3 View Code Duplication
    public function write($session_id, $session_data)
0 ignored issues
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...
110
    {
111
        try {
112 3
            $item = $this->pool->getItem($session_id);
113
114 2
            $item->set($session_data);
115
116 2
            return $this->pool->save($item);
117 1
        } catch (InvalidArgumentException $e) {
118 1
            $this->logger->error(
119 1
                $e->getMessage(),
0 ignored issues
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...
120
                [
121 1
                    'exception' => $e,
122 1
                    'session_id' => is_scalar($session_id) ? $session_id : '** Non scalar session ID **',
123 1
                    'operation' => 'write',
124
                ]
125
            );
126
127 1
            return false;
128
        }
129
    }
130
}
131