Completed
Push — master ( e29670...74e61e )
by Dominik
03:04
created

Session   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 112
Duplicated Lines 19.64 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 22
loc 112
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A has() 0 8 1
A get() 11 11 1
A set() 11 11 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 PSR7Session\Http\SessionMiddleware;
11
use PSR7Session\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
    public function __construct(LoggerInterface $logger = null)
24
    {
25
        $this->logger = $logger ?? new NullLogger();
26
    }
27
28
    /**
29
     * @param Request $request
30
     * @param string  $key
31
     *
32
     * @return bool
33
     */
34
    public function has(Request $request, string $key): bool
35
    {
36
        $exists = $this->getSession($request)->has($key);
37
38
        $this->logger->info('session: has key {key}, exists {exists}', ['key' => $key, 'exists' => $exists]);
39
40
        return $exists;
41
    }
42
43
    /**
44
     * @param Request $request
45
     * @param string  $key
46
     *
47
     * @return mixed
48
     */
49 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
        $jsonValue = $this->getSession($request)->get($key);
52
53
        $this->logger->info(
54
            'session: get key {key}, json value {jsonValue}',
55
            ['key' => $key, 'jsonValue' => $jsonValue]
56
        );
57
58
        return json_decode((string) $jsonValue, true);
59
    }
60
61
    /**
62
     * @param Request $request
63
     * @param string  $key
64
     * @param mixed   $value
65
     */
66 View Code Duplication
    public function set(Request $request, string $key, $value)
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...
67
    {
68
        $jsonValue = json_encode($value);
69
70
        $this->logger->info(
71
            'session: set key {key}, json value {jsonValue}',
72
            ['key' => $key, 'jsonValue' => $jsonValue]
73
        );
74
75
        $this->getSession($request)->set($key, $jsonValue);
76
    }
77
78
    /**
79
     * @param Request $request
80
     * @param string  $key
81
     */
82
    public function remove(Request $request, string $key)
83
    {
84
        $this->logger->info('session: remove key {key}', ['key' => $key]);
85
86
        $this->getSession($request)->remove($key);
87
    }
88
89
    /**
90
     * @param Request      $request
91
     * @param FlashMessage $flashMessage
92
     */
93
    public function addFlash(Request $request, FlashMessage $flashMessage)
94
    {
95
        $this->set($request, self::FLASH_KEY, $flashMessage);
96
    }
97
98
    /**
99
     * @param Request $request
100
     *
101
     * @return FlashMessage|null
102
     */
103
    public function getFlash(Request $request)
104
    {
105
        if (!$this->has($request, self::FLASH_KEY)) {
106
            return null;
107
        }
108
109
        $data = $this->get($request, self::FLASH_KEY);
110
        $this->remove($request, self::FLASH_KEY);
111
112
        return new FlashMessage($data[FlashMessage::JSON_KEY_TYPE], $data[FlashMessage::JSON_KEY_MESSAGE]);
113
    }
114
115
    /**
116
     * @param Request $request
117
     *
118
     * @return PSR7Session
119
     */
120
    private function getSession(Request $request): PSR7Session
121
    {
122
        return $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE);
123
    }
124
}
125