Session   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 104
Duplicated Lines 15.38 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 16
loc 104
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A has() 8 8 1
A get() 8 8 1
A set() 0 6 1
A remove() 0 6 1
A addFlash() 0 4 1
A getFlash() 0 11 2
A getSession() 0 4 1

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
declare(strict_types=1);
4
5
namespace Chubbyphp\Session;
6
7
use Psr\Http\Message\ServerRequestInterface as Request;
8
use Psr\Log\LoggerInterface;
9
use Psr\Log\NullLogger;
10
use PSR7Sessions\Storageless\Http\SessionMiddleware;
11
use PSR7Sessions\Storageless\Session\SessionInterface as PSR7Session;
12
13
final class Session implements SessionInterface
14
{
15
    /**
16
     * @var LoggerInterface
17
     */
18
    private $logger;
19
20
    /**
21
     * @param LoggerInterface $logger
22
     */
23 9
    public function __construct(LoggerInterface $logger = null)
24
    {
25 9
        $this->logger = $logger ?? new NullLogger();
26 9
    }
27
28
    /**
29
     * @param Request $request
30
     * @param string  $key
31
     *
32
     * @return bool
33
     */
34 6 View Code Duplication
    public function has(Request $request, string $key): bool
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...
35
    {
36 6
        $exists = $this->getSession($request)->has($key);
37
38 6
        $this->logger->info('session: has key {key}, exists {exists}', ['key' => $key, 'exists' => $exists]);
39
40 6
        return $exists;
41
    }
42
43
    /**
44
     * @param Request $request
45
     * @param string  $key
46
     *
47
     * @return mixed
48
     */
49 4 View Code Duplication
    public function get(Request $request, string $key)
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...
50
    {
51 4
        $value = json_decode((string) $this->getSession($request)->get($key), true);
52
53 4
        $this->logger->info('session: get key {key}, exists {exists}', ['key' => $key, 'exists' => (bool) $value]);
54
55 4
        return $value;
56
    }
57
58
    /**
59
     * @param Request $request
60
     * @param string  $key
61
     * @param mixed   $value
62
     */
63 2
    public function set(Request $request, string $key, $value)
64
    {
65 2
        $this->logger->info('session: set key {key}', ['key' => $key]);
66
67 2
        $this->getSession($request)->set($key, json_encode($value));
68 2
    }
69
70
    /**
71
     * @param Request $request
72
     * @param string  $key
73
     */
74 2
    public function remove(Request $request, string $key)
75
    {
76 2
        $this->logger->info('session: remove key {key}', ['key' => $key]);
77
78 2
        $this->getSession($request)->remove($key);
79 2
    }
80
81
    /**
82
     * @param Request      $request
83
     * @param FlashMessage $flashMessage
84
     */
85 1
    public function addFlash(Request $request, FlashMessage $flashMessage)
86
    {
87 1
        $this->set($request, self::FLASH_KEY, $flashMessage);
88 1
    }
89
90
    /**
91
     * @param Request $request
92
     *
93
     * @return FlashMessage|null
94
     */
95 2
    public function getFlash(Request $request)
96
    {
97 2
        if (!$this->has($request, self::FLASH_KEY)) {
98 1
            return null;
99
        }
100
101 1
        $data = $this->get($request, self::FLASH_KEY);
102 1
        $this->remove($request, self::FLASH_KEY);
103
104 1
        return new FlashMessage($data[FlashMessage::JSON_KEY_TYPE], $data[FlashMessage::JSON_KEY_MESSAGE]);
105
    }
106
107
    /**
108
     * @param Request $request
109
     *
110
     * @return PSR7Session
111
     */
112 9
    private function getSession(Request $request): PSR7Session
113
    {
114 9
        return $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE);
115
    }
116
}
117