Passed
Push — master ( e02931...c0b1f8 )
by Florian
06:34 queued 03:32
created

NativePhpSessionStorage::write()   A

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 3
crap 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Copyright (c) Phauthentic (https://github.com/Phauthentic)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @copyright     Copyright (c) Phauthentic (https://github.com/Phauthentic)
11
 * @link          https://github.com/Phauthentic
12
 * @license       https://opensource.org/licenses/mit-license.php MIT License
13
 */
14
namespace Phauthentic\Authentication\Authenticator\Storage;
15
16
use Psr\Http\Message\ResponseInterface;
17
use Psr\Http\Message\ServerRequestInterface;
18
19
/**
20
 * "Core php" or "native php" session adapter
21
 */
22
class NativePhpSessionStorage implements StorageInterface
23
{
24
25
    /**
26
     * @var string
27
     */
28
    protected $key = 'Auth';
29
30
    /**
31
     * Constructor
32
     *
33
     * @var string $key Key
34
     */
35 6
    public function __construct(string $sessionKey)
36
    {
37 6
        $this->key = $sessionKey;
38 6
    }
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 3
    public function clear(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
57
    {
58 3
        unset($_SESSION[$this->key]);
59
60 3
        return $response;
61
    }
62
63
    /**
64
     * {@inheritDoc}
65
     */
66 6
    public function read(ServerRequestInterface $request)
67
    {
68 6
        if (!isset($_SESSION[$this->key])) {
69 6
            return null;
70
        }
71
72 3
        return $_SESSION[$this->key];
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78 3
    public function write(ServerRequestInterface $request, ResponseInterface $response, $data): ResponseInterface
79
    {
80 3
        $_SESSION[$this->key] = (array)$data;
81
82 3
        return $response;
83
    }
84
}
85