Completed
Push — master ( 804716...5e83b7 )
by Tomasz
03:36
created

Board::transformBoard()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 4
nop 2
dl 0
loc 29
rs 9.6666
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\Profile\BoardConstruction\Builder;
8
use Aggrego\Domain\Profile\BoardTransformation\Factory as BoardTransformationFactory;
9
use Aggrego\Domain\Profile\Profile;
10
use Aggrego\Domain\ProgressiveBoard\Events\BoardCreatedEvent;
11
use Aggrego\Domain\ProgressiveBoard\Events\BoardDeletedEvent;
12
use Aggrego\Domain\ProgressiveBoard\Events\BoardTransformedEvent;
13
use Aggrego\Domain\ProgressiveBoard\Events\FinalBoardTransformedEvent;
14
use Aggrego\Domain\ProgressiveBoard\Events\ShardAddedEvent;
15
use Aggrego\Domain\ProgressiveBoard\Events\ShardUpdatedEvent;
16
use Aggrego\Domain\ProgressiveBoard\Events\UpdatedLastStepsShardEvent;
17
use Aggrego\Domain\ProgressiveBoard\Exception\UnfinishedStepPassedForTransformationException;
18
use Aggrego\Domain\ProgressiveBoard\Exception\UnprocessableBoardException;
19
use Aggrego\Domain\ProgressiveBoard\Shard\FinalItem;
20
use Aggrego\Domain\ProgressiveBoard\Step\Step;
21
use Aggrego\Domain\ProgressiveBoard\Step\Steps\FinalStep;
22
use Aggrego\Domain\ProgressiveBoard\Step\Steps\ProgressStep;
23
use Aggrego\Domain\Shared\Event\Model\TraitAggregate;
24
use Aggrego\Domain\Shared\ValueObject\Data;
25
use Aggrego\Domain\Shared\ValueObject\Key;
26
use Aggrego\Domain\Shared\ValueObject\Uuid;
27
use Aggrego\EventStore\Aggregate;
28
use Ramsey\Uuid\Uuid as RamseyUuid;
29
30
class Board implements Aggregate
31
{
32
    use TraitAggregate;
33
34
    /** @var Uuid */
35
    private $uuid;
36
37
    /** @var Key */
38
    private $key;
39
40
    /** @var Profile */
41
    private $profile;
42
43
    /** @var Step */
44
    private $step;
45
46
    /** @var bool */
47
    private $isDeleted;
48
49
    private function __construct(Uuid $uuid, Key $key, Profile $profile, Step $step)
50
    {
51
        $this->uuid = $uuid;
52
        $this->key = $key;
53
        $this->profile = $profile;
54
        $this->step = $step;
55
56
        $this->pushEvent(new BoardCreatedEvent($uuid, $key, $profile));
57
        $this->setStep($step);
58
    }
59
60
    public static function factory(Key $key, Builder $factory): self
61
    {
62
        $initialBoard = $factory->build($key);
63
        $key = $initialBoard->getKey();
64
        $profile = $initialBoard->getProfile();
65
66
        $uuid = new Uuid(
67
            RamseyUuid::uuid5(
0 ignored issues
show
Deprecated Code introduced by
The function Ramsey\Uuid\UuidInterface::toString() has been deprecated: In ramsey/uuid 4.0.0, this method will be replaced with the __toString() magic method, which is currently available in the Uuid concrete class. The new recommendation is to cast Uuid objects to string, rather than calling `toString()`. ( Ignorable by Annotation )

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

67
            /** @scrutinizer ignore-deprecated */ RamseyUuid::uuid5(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
68
                RamseyUuid::NAMESPACE_DNS,
69
                serialize($key->getValue()) . $profile
70
            )->toString()
71
        );
72
73
        return new self(
74
            $uuid,
75
            $key,
76
            $profile,
77
            $initialBoard->getStep()
78
        );
79
    }
80
81
    public static function transformBoard(Board $board, BoardTransformationFactory $transformation): Board
82
    {
83
        if ($board->isDeleted) {
84
            throw new UnprocessableBoardException();
85
        }
86
87
        if (!$board->step instanceof ProgressStep) {
88
            throw new UnprocessableBoardException();
89
        }
90
91
        if (!$board->step->readyToTransformation()) {
92
            throw new UnfinishedStepPassedForTransformationException();
93
        }
94
95
        $key = $board->key;
96
        $profile = $board->profile;
97
98
        $uuid = new Uuid(
99
            RamseyUuid::uuid5(
0 ignored issues
show
Deprecated Code introduced by
The function Ramsey\Uuid\UuidInterface::toString() has been deprecated: In ramsey/uuid 4.0.0, this method will be replaced with the __toString() magic method, which is currently available in the Uuid concrete class. The new recommendation is to cast Uuid objects to string, rather than calling `toString()`. ( Ignorable by Annotation )

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

99
            /** @scrutinizer ignore-deprecated */ RamseyUuid::uuid5(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
100
                RamseyUuid::NAMESPACE_DNS,
101
                serialize($key->getValue()) . $profile . $board->uuid->getValue()
102
            )->toString()
103
        );
104
105
        return new self(
106
            $uuid,
107
            $key,
108
            $profile,
109
            $transformation->factory($profile)->process($board->step)
110
        );
111
    }
112
113
    public function updateShard(Uuid $shardUuid, Profile $profile, Data $data): void
114
    {
115
        if ($this->isDeleted) {
116
            throw new UnprocessableBoardException();
117
        }
118
119
        if (!$this->step instanceof ProgressStep) {
120
            throw new UnprocessableBoardException();
121
        }
122
123
        $shard = new FinalItem($shardUuid, $profile, $data);
124
        $this->step->replace($shard);
125
        $this->pushEvent(new ShardUpdatedEvent($this->uuid, $shard));
126
        if ($this->step->readyToTransformation()) {
127
            $this->pushEvent(new UpdatedLastStepsShardEvent($this->uuid));
128
        }
129
    }
130
131
    public function getUuid(): Uuid
132
    {
133
        return $this->uuid;
134
    }
135
136
    public function getProfile(): Profile
137
    {
138
        return $this->profile;
139
    }
140
141
    private function setStep(Step $step): void
142
    {
143
        if ($step->getState()->isFinal()) {
144
            /** @var FinalStep $step */
145
            $this->setFinalStep($step);
146
        } else {
147
            /** @var ProgressStep $step */
148
            $this->setProgressStep($step);
149
        }
150
    }
151
152
    private function setFinalStep(FinalStep $step): void
153
    {
154
        $this->step = $step;
155
        $this->pushEvent(new FinalBoardTransformedEvent($this->uuid, $step));
156
157
        $this->isDeleted = true;
158
        $this->pushEvent(new BoardDeletedEvent($this->uuid));
159
    }
160
161
    private function setProgressStep(ProgressStep $step): void
162
    {
163
        $this->step = $step;
164
        foreach ($step->getShards() as $shard) {
165
            $this->pushEvent(new ShardAddedEvent($this->uuid, $shard));
166
        }
167
168
        /** @var ProgressStep $step */
169
        $this->pushEvent(new BoardTransformedEvent($this->uuid, $step));
170
    }
171
}
172