Passed
Push — master ( 5eb093...9c3882 )
by Tomasz
01:52
created

Board   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 37
dl 0
loc 90
c 0
b 0
f 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A updateShard() 0 5 1
A markAsDeleted() 0 4 1
A __construct() 0 10 2
A getShards() 0 3 1
A getKey() 0 3 1
A getUuid() 0 3 1
A isAllShardsFinishedProgress() 0 8 3
A factoryFromInitial() 0 17 2
A getProfile() 0 3 1
1
<?php
2
3
namespace Aggrego\Domain\Model\ProgressBoard\Entity;
4
5
use Aggrego\Domain\Event\Aggregate;
6
use Aggrego\Domain\Event\Model\Entity\TraitAggregate;
7
use Aggrego\Domain\Model\InitialBoard\Entity\Board as InitialBoard;
8
use Aggrego\Domain\Model\InitialBoard\Entity\Shard;
9
use Aggrego\Domain\Model\ProgressBoard\Events\BoardCreatedEvent;
10
use Aggrego\Domain\Model\ProgressBoard\Events\BoardDeletedEvent;
11
use Aggrego\Domain\Model\ProgressBoard\Events\ShardAddedEvent;
12
use Aggrego\Domain\Model\ProgressBoard\Events\ShardUpdatedEvent;
13
use Aggrego\Domain\Model\ProgressBoard\ValueObject\Shards;
14
use Aggrego\Domain\ValueObject\Data;
15
use Aggrego\Domain\ValueObject\Key;
16
use Aggrego\Domain\ValueObject\Profile;
17
use Aggrego\Domain\ValueObject\Source;
18
use Aggrego\Domain\ValueObject\Uuid;
19
20
class Board implements Aggregate
21
{
22
    use TraitAggregate;
23
24
    /** @var Uuid */
25
    private $uuid;
26
27
    /** @var Key */
28
    private $key;
29
30
    /** @var Profile */
31
    private $profile;
32
33
    /** @var Shards */
34
    private $shards;
35
36
    private function __construct(Uuid $uuid, Key $key, Profile $profile, Shards $shards)
37
    {
38
        $this->uuid = $uuid;
39
        $this->key = $key;
40
        $this->profile = $profile;
41
        $this->shards = $shards;
42
43
        $this->pushEvent(new BoardCreatedEvent($this));
44
        foreach ($shards as $shard) {
45
            $this->pushEvent(new ShardAddedEvent($shard));
46
        }
47
    }
48
49
    public static function factoryFromInitial(InitialBoard $board): self
50
    {
51
        $shardsList = [];
52
        /** @var Shard $shard */
53
        foreach ($board->getShards() as $shard) {
54
            $shardsList[] = new InitialShard(
55
                $shard->getUuid(),
56
                $shard->getAcceptableSource(),
57
                $shard->getKey()
58
            );
59
        }
60
61
        return new self(
62
            $board->getUuid(),
63
            $board->getKey(),
64
            $board->getProfile(),
65
            new Shards($shardsList)
66
        );
67
    }
68
69
    public function getUuid(): Uuid
70
    {
71
        return $this->uuid;
72
    }
73
74
    public function getKey(): Key
75
    {
76
        return $this->key;
77
    }
78
79
    public function getProfile(): Profile
80
    {
81
        return $this->profile;
82
    }
83
84
    public function getShards(): Shards
85
    {
86
        return $this->shards;
87
    }
88
89
    public function isAllShardsFinishedProgress(): bool
90
    {
91
        foreach ($this->shards as $shard) {
92
            if ($shard instanceof InitialShard) {
93
                return false;
94
            }
95
        }
96
        return true;
97
    }
98
99
    public function updateShard(Uuid $shardUuid, Source $source, Data $data): void
100
    {
101
        $shard = new FinalShard($shardUuid, $source, $data);
102
        $this->shards->replace($shard);
103
        $this->pushEvent(new ShardUpdatedEvent($shard));
104
    }
105
106
    public function markAsDeleted(): void
107
    {
108
        $this->isDeleted = true;
0 ignored issues
show
Bug Best Practice introduced by
The property isDeleted does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
109
        $this->pushEvent(new BoardDeletedEvent());
110
    }
111
}
112