Passed
Push — master ( 52d389...0bc1cd )
by Nicolas
02:20
created

SelfValidatedUuid::value()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Puzzle\ValueObjects;
6
7
use Puzzle\Pieces\ConvertibleToString;
8
9
class SelfValidatedUuid implements ConvertibleToString, \JsonSerializable
10
{
11
    private
12
        $uuid;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $uuid.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
13
14 12
    public function __construct(?string $uuid = null)
15
    {
16 12
        if($uuid === null)
17
        {
18 7
            $uuid = (string) \Ramsey\Uuid\Uuid::uuid4();
19
        }
20
        else
21
        {
22 8
            $this->validateUuid($uuid);
23
        }
24
25 11
        $this->uuid = $uuid;
26 11
    }
27
28 7
    public function value(): string
29
    {
30 7
        return $this->uuid;
31
    }
32
33 5
    public function equals(SelfValidatedUuid $uuid): bool
34
    {
35 5
        return $this->uuid === $uuid->value();
36
    }
37
38 1
    public function __toString(): string
39
    {
40 1
        return $this->uuid;
41
    }
42
43 8
    private function validateUuid(string $string): void
44
    {
45 8
        $validator = \Symfony\Component\Validator\Validation::createValidator();
46
47 8
        $errors = $validator->validate(
48 8
            $string,
49 8
            new \Symfony\Component\Validator\Constraints\Uuid()
50
        );
51
52 8
        if (count($errors) > 0)
53
        {
54 1
            throw new Exceptions\InvalidUuid($string);
55
        }
56 7
    }
57
58 1
    public function jsonSerialize()
59
    {
60 1
        return $this->value();
61
    }
62
}
63