1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Aggrego\Domain\Board; |
6
|
|
|
|
7
|
|
|
use Aggrego\Domain\Board\Builder as BoardBuilder; |
8
|
|
|
use Aggrego\Domain\Board\Exception\UnsupportedPrototypeBuilderException; |
9
|
|
|
use Aggrego\Domain\Profile\BoardConstruction\Factory as BoardConstructionFactory; |
10
|
|
|
use Aggrego\Domain\Profile\BoardTransformation\Factory as BoardTransformationFactory; |
11
|
|
|
use Aggrego\Domain\Profile\Profile; |
12
|
|
|
use Assert\Assertion; |
13
|
|
|
use Ramsey\Uuid\Uuid as RamseyUuid; |
14
|
|
|
|
15
|
|
|
final class Factory |
16
|
|
|
{ |
17
|
|
|
/** @var BoardBuilder[] */ |
18
|
|
|
private $builders; |
19
|
|
|
|
20
|
|
|
/** @var BoardTransformationFactory */ |
21
|
|
|
private $transformationFactory; |
22
|
|
|
|
23
|
|
|
/** @var BoardConstructionFactory */ |
24
|
|
|
private $boardConstructionFactory; |
25
|
|
|
|
26
|
|
|
public function __construct( |
27
|
|
|
array $builders, |
28
|
|
|
BoardTransformationFactory $transformationFactory, |
29
|
|
|
BoardConstructionFactory $boardConstructionFactory |
30
|
|
|
) |
31
|
|
|
{ |
32
|
|
|
Assertion::allIsInstanceOf($builders, BoardBuilder::class); |
33
|
|
|
$this->builders = $builders; |
34
|
|
|
$this->transformationFactory = $transformationFactory; |
35
|
|
|
$this->boardConstructionFactory = $boardConstructionFactory; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function newBoard(Key $key, Profile $profile): Board |
39
|
|
|
{ |
40
|
|
|
$factory = $this->boardConstructionFactory->factory($profile); |
41
|
|
|
$prototype = $factory->build($key); |
42
|
|
|
$key = $prototype->getKey(); |
43
|
|
|
$profile = $prototype->getProfile(); |
44
|
|
|
|
45
|
|
|
$uuid = new Uuid( |
46
|
|
|
RamseyUuid::uuid5( |
|
|
|
|
47
|
|
|
RamseyUuid::NAMESPACE_DNS, |
48
|
|
|
serialize($key->getValue()) . $profile |
49
|
|
|
)->toString() |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
foreach ($this->builders as $builder) { |
53
|
|
|
if ($builder->isSupported($prototype)) { |
54
|
|
|
continue; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $builder->build( |
58
|
|
|
$uuid, |
59
|
|
|
$prototype->getKey(), |
60
|
|
|
$prototype->getProfile(), |
61
|
|
|
$prototype->getMetadata(), |
62
|
|
|
null |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
throw new UnsupportedPrototypeBuilderException(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function fromBoard(Board $board): Board |
70
|
|
|
{ |
71
|
|
|
$transformation = $this->transformationFactory->factory($board->getProfile()); |
72
|
|
|
$prototype = $transformation->transform($board); |
73
|
|
|
|
74
|
|
|
$key = $prototype->getKey(); |
75
|
|
|
$profile = $prototype->getProfile(); |
76
|
|
|
|
77
|
|
|
$parentUuid = $board->getUuid(); |
78
|
|
|
$uuid = new Uuid( |
79
|
|
|
RamseyUuid::uuid5( |
|
|
|
|
80
|
|
|
RamseyUuid::NAMESPACE_DNS, |
81
|
|
|
serialize($key->getValue()) . $profile . $parentUuid->getValue() |
82
|
|
|
)->toString() |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
foreach ($this->builders as $builder) { |
86
|
|
|
if (!$builder->isSupported($prototype)) { |
87
|
|
|
continue; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $builder->build( |
91
|
|
|
$uuid, |
92
|
|
|
$prototype->getKey(), |
93
|
|
|
$prototype->getProfile(), |
94
|
|
|
$prototype->getMetadata(), |
95
|
|
|
$parentUuid |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
throw new UnsupportedPrototypeBuilderException(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
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.