NativePhpSessionStorage::write()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 2
c 2
b 0
f 0
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
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
15
declare(strict_types=1);
16
17
namespace Phauthentic\Authentication\Authenticator\Storage;
18
19
use Psr\Http\Message\ResponseInterface;
20
use Psr\Http\Message\ServerRequestInterface;
21
22
/**
23
 * "Core php" or "native php" session adapter
24
 */
25
class NativePhpSessionStorage implements StorageInterface
26
{
27
    /**
28
     * @var string
29
     */
30
    protected $key = 'Auth';
31
32
    /**
33
     * Constructor
34
     *
35 8
     * @param string $sessionKey Session key.
36
     */
37 8
    public function __construct(string $sessionKey)
38 8
    {
39
        $this->key = $sessionKey;
40
    }
41
42
    /**
43
     * Set session key for stored identity.
44
     *
45
     * @param string $key Session key.
46
     * @return $this
47
     */
48
    public function setKey(string $key): self
49
    {
50
        $this->key = $key;
51
52
        return $this;
53
    }
54
55
    /**
56 4
     * {@inheritDoc}
57
     */
58 4
    public function clear(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
59
    {
60 4
        unset($_SESSION[$this->key]);
61
62
        return $response;
63
    }
64
65
    /**
66 8
     * {@inheritDoc}
67
     */
68 8
    public function read(ServerRequestInterface $request)
69 8
    {
70
        if (!isset($_SESSION[$this->key])) {
71
            return null;
72 4
        }
73
74
        return $_SESSION[$this->key];
75
    }
76
77
    /**
78 4
     * {@inheritDoc}
79
     */
80 4
    public function write(ServerRequestInterface $request, ResponseInterface $response, $data): ResponseInterface
81
    {
82 4
        $_SESSION[$this->key] = (array)$data;
83
84
        return $response;
85
    }
86
}
87