Completed
Push — master ( af0cec...4d4875 )
by Tomasz
01:43
created

FromBoardFactory::fromBoard()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 31
rs 9.6
c 0
b 0
f 0
cc 3
nc 3
nop 1
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\AggregateEventConsumer\Uuid;
17
use Aggrego\Domain\Board\Builder as BoardBuilder;
18
use Aggrego\Domain\Board\Exception\UnsupportedPrototypeBuilderException;
19
use Aggrego\Domain\Profile\BoardTransformation\Factory as BoardTransformationFactory;
20
use Assert\Assertion;
21
use Ramsey\Uuid\Uuid as RamseyUuid;
22
23
class FromBoardFactory
24
{
25
    /** @var BoardBuilder[] */
26
    private $builders;
27
28
    /** @var BoardTransformationFactory */
29
    private $transformationFactory;
30
31
    public function __construct(
32
        array $builders,
33
        BoardTransformationFactory $transformationFactory
34
    )
35
    {
36
        Assertion::allIsInstanceOf($builders, BoardBuilder::class);
37
        $this->builders = $builders;
38
        $this->transformationFactory = $transformationFactory;
39
    }
40
41
    public function fromBoard(Board $board): Board
42
    {
43
        $transformation = $this->transformationFactory->factory($board->getProfile());
44
        $prototype = $transformation->transform($board);
45
46
        $key = $prototype->getKey();
47
        $profile = $prototype->getProfile();
48
49
        $parentUuid = $board->getUuid();
50
        $uuid = new Uuid(
51
            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

51
            /** @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...
52
                RamseyUuid::NAMESPACE_DNS,
53
                serialize($key->getValue()) . $profile . $parentUuid->getValue()
54
            )->toString()
55
        );
56
57
        foreach ($this->builders as $builder) {
58
            if (!$builder->isSupported($prototype)) {
59
                continue;
60
            }
61
62
            return $builder->build(
63
                $uuid,
64
                $prototype->getKey(),
65
                $prototype->getProfile(),
66
                $prototype->getMetadata(),
67
                $parentUuid
68
            );
69
        }
70
71
        throw new UnsupportedPrototypeBuilderException();
72
    }
73
}
74