BaseId::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace BaseValueObject\Scalar;
4
5
use Ramsey\Uuid\Uuid;
6
use Webmozart\Assert\Assert;
7
8
/**
9
 * Class BaseId
10
 * @package BaseValueObject\Scalar
11
 */
12
abstract class BaseId extends BaseScalar
13
{
14
    protected $value;
15
16
    /**
17
     * BaseId constructor.
18
     *
19
     * @param null|string $id
20
     */
21 12
    public function __construct(?string $id = null)
22
    {
23 12
        $this->setValue($id);
24 9
    }
25
26 12
    private function setValue(?string $id): void
27
    {
28 12
        if ($id === null) {
29 3
            $this->value = Uuid::uuid4()->toString();
30 3
            return;
31
        }
32
33 9
        Assert::uuid($id);
34 6
        $this->value = $id;
35 6
    }
36
37
    /**
38
     * {@inheritdoc}
39
     *
40
     * @return string
41
     */
42 9
    public function value(): string
43
    {
44 9
        return $this->value;
45
    }
46
47
    /**
48
     * @return string
49
     */
50 3
    public function __toString(): string
51
    {
52 3
        return $this->value();
53
    }
54
}
55