Passed
Push — master ( 76e429...e854a7 )
by Florian
02:44
created

CorrelationID::sameAs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright (c) Florian Krämer (https://florian-kraemer.net)
5
 * Licensed under The GPL3 License
6
 * For full copyright and license information, please see the LICENSE.txt
7
 * Redistributions of files must retain the above copyright notice.
8
 *
9
 * @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net)
10
 * @author    Florian Krämer
11
 * @link      https://github.com/Phauthentic
12
 * @license   https://opensource.org/licenses/MIT MIT License
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phauthentic\Infrastructure\Utils;
18
19
use Ramsey\Uuid\Uuid;
20
use Ramsey\Uuid\UuidInterface;
21
22
class CorrelationID
23
{
24
    /**
25
     * @var string The correlation ID.
26
     */
27
    protected static $uuid;
28
29
    /**
30
     * Turns this object into an UUID string.
31
     *
32
     * @return string
33
     * @throws \Exception
34
     */
35 2
    public static function toString(): string
36
    {
37 2
        if (empty(static::$uuid)) {
38 1
            static::$uuid = static::generate()->toString();
39
        }
40
41 2
        return static::$uuid;
42
    }
43
44
    /**
45
     * Compares the current id against another
46
     *
47
     * @param string $otherID Other ID
48
     * @return boolean
49
     */
50 1
    public static function sameAs(string $otherID): bool
51
    {
52 1
        return static::$uuid === $otherID;
53
    }
54
55
    /**
56
     * Generates a new correlation ID.
57
     *
58
     * @return \Ramsey\Uuid\UuidInterface
59
     * @throws \Exception
60
     */
61 1
    protected static function generate(): UuidInterface
62
    {
63 1
        return Uuid::uuid4();
64
    }
65
66
    /**
67
     * Constructor.
68
     *
69
     * @codeCoverageIgnore
70
     */
71
    private function __construct()
72
    {
73
    }
74
}
75