|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace TomCizek\ResponseRecorder\Application\ResponseRecorder\MutationResponse; |
|
4
|
|
|
|
|
5
|
|
|
use Prooph\Common\Messaging\DomainEvent; |
|
6
|
|
|
use TomCizek\ResponseRecorder\Application\ResponseRecorder\BasicMutationResponse; |
|
7
|
|
|
use TomCizek\ResponseRecorder\Application\ResponseRecorder\MutationResponse\ErrorCollection\GenericShowableMutationError; |
|
8
|
|
|
use TomCizek\ResponseRecorder\Application\ResponseRecorder\MutationResponse\ErrorCollection\ShowableMutationError; |
|
9
|
|
|
use TomCizek\ResponseRecorder\DomainModel\TranslatableDomainErrorEvent; |
|
10
|
|
|
|
|
11
|
|
|
class BasicMutationResponseBuilder |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var DomainEvent[] */ |
|
14
|
|
|
private $domainEvents = []; |
|
15
|
|
|
|
|
16
|
|
|
/** @var ShowableMutationError[] */ |
|
17
|
|
|
private $errors = []; |
|
18
|
|
|
|
|
19
|
9 |
|
private function __construct() |
|
20
|
|
|
{ |
|
21
|
9 |
|
} |
|
22
|
|
|
|
|
23
|
9 |
|
public static function create(): self |
|
24
|
|
|
{ |
|
25
|
9 |
|
return new self(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
9 |
|
public function build(): BasicMutationResponse |
|
29
|
|
|
{ |
|
30
|
9 |
|
return BasicMutationResponse::create( |
|
31
|
9 |
|
new DomainEventCollection($this->domainEvents), |
|
32
|
9 |
|
new MutationErrorCollection($this->errors) |
|
33
|
|
|
); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
3 |
|
public function addDomainEvent(DomainEvent $domainEvent): void |
|
37
|
|
|
{ |
|
38
|
3 |
|
$this->domainEvents[] = $domainEvent; |
|
39
|
|
|
|
|
40
|
3 |
|
if ($domainEvent instanceof TranslatableDomainErrorEvent) { |
|
41
|
2 |
|
$this->errors[] = GenericShowableMutationError::create( |
|
42
|
2 |
|
$domainEvent->getErrorMessage(), |
|
43
|
2 |
|
$domainEvent->getErrorMessageParams() |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
3 |
|
} |
|
47
|
|
|
|
|
48
|
5 |
|
public function addError(ShowableMutationError $showableMutationError): void |
|
49
|
|
|
{ |
|
50
|
5 |
|
$this->errors[] = $showableMutationError; |
|
51
|
5 |
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|