Completed
Push — master ( 2f9adf...658dc6 )
by Tomasz
01:44
created

Board::getUuid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Aggrego\Domain\ProgressiveBoard;
6
7
use Aggrego\Domain\Api\Application\Event\Aggregate;
8
use Aggrego\Domain\Profile\BoardConstruction\Builder;
9
use Aggrego\Domain\Profile\BoardTransformation\Transformation;
10
use Aggrego\Domain\Profile\Profile;
11
use Aggrego\Domain\ProgressiveBoard\Events\BoardCreatedEvent;
12
use Aggrego\Domain\ProgressiveBoard\Events\BoardDeletedEvent;
13
use Aggrego\Domain\ProgressiveBoard\Events\BoardTransformedEvent;
14
use Aggrego\Domain\ProgressiveBoard\Events\FinalBoardTransformedEvent;
15
use Aggrego\Domain\ProgressiveBoard\Events\ShardAddedEvent;
16
use Aggrego\Domain\ProgressiveBoard\Events\ShardUpdatedEvent;
17
use Aggrego\Domain\ProgressiveBoard\Events\UpdatedLastStepsShardEvent;
18
use Aggrego\Domain\ProgressiveBoard\Exception\UnfinishedStepPassedForTransformationException;
19
use Aggrego\Domain\ProgressiveBoard\Exception\UnprocessableBoardException;
20
use Aggrego\Domain\ProgressiveBoard\Shard\Collection;
21
use Aggrego\Domain\ProgressiveBoard\Shard\FinalItem;
22
use Aggrego\Domain\ProgressiveBoard\Shard\InitialItem;
23
use Aggrego\Domain\ProgressiveBoard\Step\State;
24
use Aggrego\Domain\ProgressiveBoard\Step\Step;
25
use Aggrego\Domain\ProgressiveBoard\Step\Steps\FinalStep;
26
use Aggrego\Domain\ProgressiveBoard\Step\Steps\ProgressStep;
27
use Aggrego\Domain\Shared\Event\Model\TraitAggregate;
28
use Aggrego\Domain\Shared\ValueObject\Data;
29
use Aggrego\Domain\Shared\ValueObject\Key;
30
use Aggrego\Domain\Shared\ValueObject\Uuid;
31
32
class Board implements Aggregate
33
{
34
    use TraitAggregate;
35
36
    /** @var Uuid */
37
    private $uuid;
38
39
    /** @var Key */
40
    private $key;
41
42
    /** @var Profile */
43
    private $profile;
44
45
    /** @var Step */
46
    private $step;
47
48
    /** @var bool */
49
    private $isDeleted;
50
51
    private function __construct(Uuid $uuid, Key $key, Profile $profile, ProgressStep $step)
52
    {
53
        $this->uuid = $uuid;
54
        $this->key = $key;
55
        $this->profile = $profile;
56
        $this->step = $step;
57
58
        $this->pushEvent(new BoardCreatedEvent($uuid, $key, $profile));
59
        $this->setStep($step);
60
    }
61
62
    public static function factory(Key $key, Builder $factory): self
63
    {
64
        $initialBoard = $factory->build($key);
65
66
        $shardsList = [];
67
        /** @var InitialItem $item */
68
        foreach ($initialBoard->getShards() as $item) {
69
            $shardsList[] = new InitialItem(
70
                $item->getUuid(),
71
                $item->getProfile(),
72
                $item->getKey()
73
            );
74
        }
75
76
        return new self(
77
            $initialBoard->getUuid(),
78
            $initialBoard->getKey(),
79
            $initialBoard->getProfile(),
80
            new ProgressStep(State::createInitial(), new Collection($shardsList))
81
        );
82
    }
83
84
    public function getUuid(): Uuid
85
    {
86
        return $this->uuid;
87
    }
88
89
    public function getProfile(): Profile
90
    {
91
        return $this->profile;
92
    }
93
94
    public function updateShard(Uuid $shardUuid, Profile $profile, Data $data): void
95
    {
96
        if ($this->isDeleted) {
97
            throw new UnprocessableBoardException();
98
        }
99
        $shard = new FinalItem($shardUuid, $profile, $data);
100
        $this->step->replace($shard);
0 ignored issues
show
Bug introduced by
The method replace() does not exist on Aggrego\Domain\ProgressiveBoard\Step\Step. It seems like you code against a sub-type of Aggrego\Domain\ProgressiveBoard\Step\Step such as Aggrego\Domain\Progressi...Step\Steps\ProgressStep. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
        $this->step->/** @scrutinizer ignore-call */ 
101
                     replace($shard);
Loading history...
101
        $this->pushEvent(new ShardUpdatedEvent($this->uuid, $shard));
102
        if ($this->isStepReadyForNextTransformation()) {
103
            $this->pushEvent(new UpdatedLastStepsShardEvent($this->uuid));
104
        }
105
    }
106
107
    public function isStepReadyForNextTransformation(): bool
108
    {
109
        if ($this->isDeleted) {
110
            return false;
111
        }
112
        return $this->step->isReadyForTransformation();
0 ignored issues
show
Bug introduced by
The method isReadyForTransformation() does not exist on Aggrego\Domain\ProgressiveBoard\Step\Step. It seems like you code against a sub-type of Aggrego\Domain\ProgressiveBoard\Step\Step such as Aggrego\Domain\Progressi...Step\Steps\ProgressStep. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

112
        return $this->step->/** @scrutinizer ignore-call */ isReadyForTransformation();
Loading history...
113
    }
114
115
    public function transformStep(Transformation $transformation): void
116
    {
117
        if ($this->isDeleted) {
118
            throw new UnprocessableBoardException();
119
        }
120
121
        if (!$this->isStepReadyForNextTransformation()) {
122
            throw new UnfinishedStepPassedForTransformationException();
123
        }
124
125
        $step = $transformation->process($this->step);
126
127
        if ($step->getState()->isFinal()) {
128
            /** @var FinalStep $step */
129
            $this->pushEvent(new FinalBoardTransformedEvent($this->uuid, $step));
130
131
            $this->isDeleted = true;
132
            $this->pushEvent(new BoardDeletedEvent($this->uuid));
133
        } else {
134
            /** @var ProgressStep $step */
135
            $this->pushEvent(new BoardTransformedEvent($this->uuid, $step));
136
            $this->setStep($step);
137
        }
138
    }
139
140
    private function setStep(ProgressStep $step): void
141
    {
142
        $this->step = $step;
143
        foreach ($step->getShards() as $shard) {
144
            $this->pushEvent(new ShardAddedEvent($this->uuid, $shard));
145
        }
146
    }
147
}
148