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

NewBoardFactory::newBoard()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 29
rs 9.6333
c 0
b 0
f 0
cc 3
nc 3
nop 2
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\BoardConstruction\Factory as BoardConstructionFactory;
20
use Aggrego\Domain\Profile\Profile;
21
use Assert\Assertion;
22
use Ramsey\Uuid\Uuid as RamseyUuid;
23
24
class NewBoardFactory
25
{
26
    /** @var BoardBuilder[] */
27
    private $builders;
28
29
    /** @var BoardConstructionFactory */
30
    private $boardConstructionFactory;
31
32
    public function __construct(
33
        array $builders,
34
        BoardConstructionFactory $boardConstructionFactory
35
    )
36
    {
37
        Assertion::allIsInstanceOf($builders, BoardBuilder::class);
38
        $this->builders = $builders;
39
        $this->boardConstructionFactory = $boardConstructionFactory;
40
    }
41
42
    public function newBoard(Key $key, Profile $profile): Board
43
    {
44
        $factory = $this->boardConstructionFactory->factory($profile);
45
        $prototype = $factory->build($key);
46
        $key = $prototype->getKey();
47
        $profile = $prototype->getProfile();
48
49
        $uuid = new Uuid(
50
            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

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