Passed
Push — master ( 2ee0d1...1e8995 )
by Tomasz
03:21
created

Board::produceUuid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Aggrego\Domain\Api\Application\Profile\BoardConstruction\InitialBoardModel;
6
7
use Aggrego\Domain\Api\Application\Profile\BoardConstruction\InitialBoardModel\Shard\Collection;
8
use Aggrego\Domain\Api\Application\Profile\BoardConstruction\InitialBoardModel\Shard\Item;
9
use Aggrego\Domain\Profile\Profile;
10
use Aggrego\Domain\Shared\ValueObject\Key;
11
use Aggrego\Domain\Shared\ValueObject\Uuid;
12
use Ramsey\Uuid\Uuid as RamseyUuid;
13
use Traversable;
14
15
class Board
16
{
17
    /** @var Uuid */
18
    private $uuid;
19
20
    /** @var Key */
21
    private $key;
22
23
    /** @var Profile */
24
    private $profile;
25
26
    /** @var Collection */
27
    private $shards;
28
29
    protected function __construct(Key $key, Profile $profile)
30
    {
31
        $this->uuid = $this->produceUuid($key, $profile);
32
        $this->key = $key;
33
        $this->profile = $profile;
34
        $this->shards = new Collection();
35
    }
36
37
    public function getUuid(): Uuid
38
    {
39
        return $this->uuid;
40
    }
41
42
    public function getKey(): Key
43
    {
44
        return $this->key;
45
    }
46
47
    public function getProfile(): Profile
48
    {
49
        return $this->profile;
50
    }
51
52
    public function getShards(): Traversable
53
    {
54
        return $this->shards->getIterator();
55
    }
56
57
    public function addShard(Key $key, Profile $shardProfile): void
58
    {
59
        $this->shards->add(
60
            new Item(
61
                $this->produceShardUuid($key, $shardProfile),
62
                $shardProfile,
63
                $key
64
            )
65
        );
66
    }
67
68
    private function produceUuid(Key $key, Profile $profile): Uuid
69
    {
70
        return new Uuid(
71
            RamseyUuid::uuid5(
72
                RamseyUuid::NAMESPACE_DNS,
73
                serialize($key->getValue()) . $profile
74
            )->toString()
75
        );
76
    }
77
78
    private function produceShardUuid(Key $key, Profile $shardProfile): Uuid
79
    {
80
        return new Uuid(
81
            RamseyUuid::uuid5(
82
                RamseyUuid::NAMESPACE_DNS,
83
                serialize($key->getValue()) . $shardProfile . $this->getUuid()->getValue()
84
            )->toString()
85
        );
86
    }
87
}
88