CakeSessionStorage::clear()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 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