1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of the Aggrego. |
5
|
|
|
* (c) Tomasz Kunicki <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types = 1); |
13
|
|
|
|
14
|
|
|
namespace Aggrego\Domain\Board; |
15
|
|
|
|
16
|
|
|
use Aggrego\Domain\Board\Builder as BoardBuilder; |
17
|
|
|
use Aggrego\Domain\Board\Exception\UnsupportedPrototypeBuilderException; |
18
|
|
|
use Aggrego\Domain\Profile\BoardTransformation\Factory as BoardTransformationFactory; |
19
|
|
|
use Assert\Assertion; |
20
|
|
|
use Ramsey\Uuid\Uuid as RamseyUuid; |
21
|
|
|
|
22
|
|
|
class FromBoardFactory |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var BoardBuilder[] |
26
|
|
|
*/ |
27
|
|
|
private $builders; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var BoardTransformationFactory |
31
|
|
|
*/ |
32
|
|
|
private $transformationFactory; |
33
|
|
|
|
34
|
|
|
public function __construct( |
35
|
|
|
array $builders, |
36
|
|
|
BoardTransformationFactory $transformationFactory |
37
|
|
|
) { |
38
|
|
|
Assertion::allIsInstanceOf($builders, BoardBuilder::class); |
39
|
|
|
$this->builders = $builders; |
40
|
|
|
$this->transformationFactory = $transformationFactory; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function fromBoard(Key $key, Board $board): Board |
44
|
|
|
{ |
45
|
|
|
$transformation = $this->transformationFactory->factory($board->getProfile()); |
46
|
|
|
$prototype = $transformation->transform($key, $board); |
47
|
|
|
|
48
|
|
|
$key = $prototype->getKey(); |
49
|
|
|
$profile = $prototype->getProfile(); |
50
|
|
|
|
51
|
|
|
$parentUuid = $board->getUuid(); |
52
|
|
|
$uuid = new Uuid( |
53
|
|
|
RamseyUuid::uuid5( |
54
|
|
|
RamseyUuid::NAMESPACE_DNS, |
55
|
|
|
serialize($key->getValue()) . $profile . $parentUuid->getValue() |
56
|
|
|
)->toString() |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
foreach ($this->builders as $builder) { |
60
|
|
|
if (!$builder->isSupported($prototype)) { |
61
|
|
|
continue; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $builder->build( |
65
|
|
|
$uuid, |
66
|
|
|
$prototype->getKey(), |
67
|
|
|
$prototype->getProfile(), |
68
|
|
|
$prototype->getMetadata(), |
69
|
|
|
$parentUuid |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
throw new UnsupportedPrototypeBuilderException(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|