CakeSessionStorage   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 62.5%

Importance

Changes 0
Metric Value
wmc 6
eloc 13
dl 0
loc 76
ccs 10
cts 16
cp 0.625
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getSession() 0 3 1
A clear() 0 5 1
A setAttribute() 0 5 1
A write() 0 5 1
A setKey() 0 5 1
A read() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Phauthentic\Authentication\Authenticator\Storage;
5
6
use Phauthentic\Authentication\Authenticator\Storage\StorageInterface;
7
use Cake\Http\Session;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
11
/**
12
 * Storage adapter for the CakePHP Session
13
 */
14
class CakeSessionStorage implements StorageInterface
15
{
16
17
    /**
18
     * @var string
19
     */
20
    protected $key = 'Auth';
21
22
    /**
23
     * @var string
24
     */
25
    protected $attribute = 'session';
26
27
    /**
28
     * Set request attribute name for a session object.
29
     *
30
     * @param string $attribute Request attribute name.
31
     * @return $this
32
     */
33
    public function setAttribute(string $attribute): self
34
    {
35
        $this->attribute = $attribute;
36
37
        return $this;
38
    }
39
40
    /**
41
     * Set session key for stored identity.
42
     *
43
     * @param string $key Session key.
44
     * @return $this
45
     */
46
    public function setKey(string $key): self
47
    {
48
        $this->key = $key;
49
50
        return $this;
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56 1
    public function clear(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
57
    {
58 1
        $this->getSession($request)->delete($this->key);
59
60 1
        return $response;
61
    }
62
63
    /**
64
     * {@inheritDoc}
65
     */
66 1
    public function read(ServerRequestInterface $request)
67
    {
68 1
        return $this->getSession($request)->read($this->key);
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74 1
    public function write(ServerRequestInterface $request, ResponseInterface $response, $data): ResponseInterface
75
    {
76 1
        $this->getSession($request)->write($this->key, $data);
77
78 1
        return $response;
79
    }
80
81
    /**
82
     * Returns session object.
83
     *
84
     * @param ServerRequestInterface $request Request.
85
     * @return Session
86
     */
87 3
    protected function getSession(ServerRequestInterface $request): Session
88
    {
89 3
        return $request->getAttribute($this->attribute);
90
    }
91
}
92