NewBoardFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 25
c 1
b 0
f 0
dl 0
loc 50
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A newBoard() 0 29 3
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\BoardConstruction\Factory as BoardConstructionFactory;
19
use Aggrego\Domain\Profile\Profile;
20
use Assert\Assertion;
21
use Ramsey\Uuid\Uuid as RamseyUuid;
22
23
class NewBoardFactory
24
{
25
    /**
26
     * @var BoardBuilder[]
27
     */
28
    private $builders;
29
30
    /**
31
     * @var BoardConstructionFactory
32
     */
33
    private $boardConstructionFactory;
34
35
    public function __construct(
36
        array $builders,
37
        BoardConstructionFactory $boardConstructionFactory
38
    ) {
39
        Assertion::allIsInstanceOf($builders, BoardBuilder::class);
40
        $this->builders = $builders;
41
        $this->boardConstructionFactory = $boardConstructionFactory;
42
    }
43
44
    public function newBoard(Key $key, Profile $profile): Board
45
    {
46
        $factory = $this->boardConstructionFactory->factory($profile);
47
        $prototype = $factory->build($key);
48
        $key = $prototype->getKey();
49
        $profile = $prototype->getProfile();
50
51
        $uuid = new Uuid(
52
            RamseyUuid::uuid5(
53
                RamseyUuid::NAMESPACE_DNS,
54
                serialize($key->getValue()) . $profile
55
            )->toString()
56
        );
57
58
        foreach ($this->builders as $builder) {
59
            if (!$builder->isSupported($prototype)) {
60
                continue;
61
            }
62
63
            return $builder->build(
64
                $uuid,
65
                $prototype->getKey(),
66
                $prototype->getProfile(),
67
                $prototype->getMetadata(),
68
                null
69
            );
70
        }
71
72
        throw new UnsupportedPrototypeBuilderException();
73
    }
74
}
75