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

CorrelationID   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 51
ccs 8
cts 8
cp 1
rs 10
c 1
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toString() 0 7 2
A generate() 0 3 1
A __construct() 0 2 1
A sameAs() 0 3 1
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