Test Failed
Pull Request — master (#11)
by Florian
07:04 queued 04:09
created

NativePhpSessionStorage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 12
dl 0
loc 60
ccs 12
cts 15
cp 0.8
rs 10
c 2
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A write() 0 5 1
A setKey() 0 5 1
A read() 0 7 2
A clear() 0 5 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
     * @param string $sessionKey Session key.
36
     */
37 2
    public function __construct(string $sessionKey)
38
    {
39 2
        $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
     * {@inheritDoc}
57
     */
58 1
    public function clear(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
59
    {
60 1
        unset($_SESSION[$this->key]);
61
62 1
        return $response;
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68 2
    public function read(ServerRequestInterface $request)
69
    {
70 2
        if (!isset($_SESSION[$this->key])) {
71 2
            return null;
72
        }
73
74 1
        return $_SESSION[$this->key];
75
    }
76
77
    /**
78
     * {@inheritDoc}
79
     */
80 1
    public function write(ServerRequestInterface $request, ResponseInterface $response, $data): ResponseInterface
81
    {
82 1
        $_SESSION[$this->key] = (array)$data;
83
84 1
        return $response;
85
    }
86
}
87