ExtractUniqueKeyFromSession   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 29
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 14 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PSR7Csrf\Session;
6
7
use PSR7Sessions\Storageless\Session\SessionInterface;
8
9
final class ExtractUniqueKeyFromSession implements ExtractUniqueKeyFromSessionInterface
10
{
11
    const ENTROPY = 32;
12
13
    /**
14
     * @var string
15
     */
16
    private $uniqueIdKey;
17
18
    public function __construct(string $uniqueIdKey)
19 12
    {
20
        $this->uniqueIdKey = $uniqueIdKey;
21 12
    }
22 12
23
    public function __invoke(SessionInterface $session) : string
24 12
    {
25
        $uniqueKey = $session->get($this->uniqueIdKey, '');
26 12
27
        if ('' === $uniqueKey || ! is_string($uniqueKey)) {
28 12
            $generatedKey = bin2hex(random_bytes(self::ENTROPY));
29 8
30
            $session->set($this->uniqueIdKey, $generatedKey);
31 8
32
            return $generatedKey;
33 8
        }
34
35
        return $uniqueKey;
36 4
    }
37
}
38