Runtime   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 41
ccs 8
cts 10
cp 0.8
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 7 2
A regenerate() 0 7 1
A forget() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of Lenius Basket, a PHP package to handle
5
 * your shopping basket.
6
 *
7
 * Copyright (c) 2017 Lenius.
8
 * https://github.com/lenius/basket
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 *
13
 * @author Carsten Jonstrup<[email protected]>
14
 * @copyright 2017 Lenius.
15
 *
16
 * @version production
17
 *
18
 * @see https://github.com/lenius/basket
19
 */
20
21
namespace Lenius\Basket\Identifier;
22
23
use Lenius\Basket\IdentifierInterface;
24
25
/**
26
 * Class Runtime.
27
 */
28
class Runtime implements IdentifierInterface
29
{
30
    /** @var string */
31
    protected static string $identifier = '';
32
33
    /**
34
     * Get the current or new unique identifier.
35
     *
36
     * @return string The identifier
37
     */
38 18
    public function get(): string
39
    {
40 18
        if (! empty(static::$identifier)) {
41 17
            return static::$identifier;
42
        }
43
44 1
        return $this->regenerate();
45
    }
46
47
    /**
48
     * Regenerate the identifier.
49
     *
50
     * @return string The identifier
51
     */
52 1
    public function regenerate(): string
53
    {
54 1
        $identifier = md5(uniqid('', true));
55
56 1
        static::$identifier = $identifier;
57
58 1
        return $identifier;
59
    }
60
61
    /**
62
     * Forget the identifier.
63
     *
64
     * @return void
65
     */
66
    public function forget(): void
67
    {
68
        static::$identifier = '';
69
    }
70
}
71