AggregateRootIdentifier   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 3
dl 0
loc 91
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A wrappedUuid() 0 4 1
A equalsTo() 0 5 3
A jsonSerialize() 0 4 1
A __toString() 0 4 1
A createFromString() 0 10 2
1
<?php
2
3
/**
4
 * This file is part of slick/cqrs-tools
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\CQRSTools\Domain;
11
12
use Exception;
13
use JsonSerializable;
14
use Ramsey\Uuid\Uuid;
15
use Ramsey\Uuid\UuidInterface;
16
use Slick\CQRSTools\Domain\Common\Comparable;
17
use Slick\CQRSTools\Domain\Common\Stringable;
18
use Slick\CQRSTools\Domain\Exception\IdentifierCreationException;
19
20
/**
21
 * Aggregate Root Identifier
22
 *
23
 * @package Slick\CQRSTools\Domain
24
 */
25
abstract class AggregateRootIdentifier implements Stringable, Comparable, JsonSerializable
26
{
27
28
    /**
29
     * @var UuidInterface
30
     */
31
    protected $uuid;
32
33
    /**
34
     * Creates an AggregateRootIdentifier
35
     *
36
     * @param string|null $uuidStr
37
     *
38
     * @throws IdentifierCreationException when internal UUID cannot be created
39
     */
40
    public function __construct(string $uuidStr = null)
41
    {
42
        try {
43
            $this->uuid = Uuid::uuid4();
44
            if ($uuidStr) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $uuidStr of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
45
                $this->uuid = $this->createFromString($uuidStr);
46
            }
47
        } catch (Exception $caught) {
48
            throw new IdentifierCreationException($caught->getMessage(), 1, $caught);
49
        }
50
    }
51
52
    /**
53
     * Internal UUID
54
     *
55
     * @return UuidInterface
56
     */
57
    public function wrappedUuid(): UuidInterface
58
    {
59
        return $this->uuid;
60
    }
61
62
    /**
63
     * Compares object for equality
64
     *
65
     * @param mixed $other
66
     *
67
     * @return bool
68
     */
69
    public function equalsTo($other): bool
70
    {
71
        $sameType = is_object($other) && is_a($other, get_called_class());
72
        return $sameType && $this->uuid->equals($other->uuid);
73
    }
74
75
    /**
76
     * String version of UUID that will be converted to a JSON string
77
     *
78
     * @return string data which can be serialized by json_encode(),
79
     *               which is a value of any type other than a resource.
80
     */
81
    public function jsonSerialize()
82
    {
83
        return (string) $this->uuid;
84
    }
85
86
    /**
87
     * String representation of the object
88
     *
89
     * @return string
90
     */
91
    public function __toString()
92
    {
93
        return (string) $this->uuid;
94
    }
95
96
    /**
97
     * Creates an UUID with provided UUID string
98
     *
99
     * @param string $uuidStr
100
     *
101
     * @return UuidInterface
102
     *
103
     * @throws IdentifierCreationException If the provided text is not a valid UUID string
104
     */
105
    protected function createFromString(string $uuidStr): UuidInterface
106
    {
107
        if (!Uuid::isValid($uuidStr)) {
108
            throw new IdentifierCreationException(
109
                "Invalid aggregate root ID '{$uuidStr}'."
110
            );
111
        }
112
113
        return Uuid::fromString($uuidStr);
114
    }
115
}
116