Passed
Push — master ( 5e83b7...81caf8 )
by Tomasz
02:02
created

Factory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 10
rs 10
c 0
b 0
f 0
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(
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

46
            /** @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...
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(
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

79
            /** @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...
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