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; |
|
|
|
|
109
|
|
|
$this->pushEvent(new BoardDeletedEvent()); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|