Transformation::transform()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 2
dl 0
loc 18
rs 9.8666
c 0
b 0
f 0
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\BasicBlockDomainProfile\Domain\Profile\BoardTransformation;
15
16
use Aggrego\DataDomainBoard\Board\Board as DataBoard;
17
use Aggrego\DataDomainBoard\Board\Data;
18
use Aggrego\DataDomainBoard\Board\Prototype\Board as DataBoardPrototype;
19
use Aggrego\Domain\Board\Board;
20
use Aggrego\Domain\Board\Key;
21
use Aggrego\Domain\Board\Prototype\Board as BoardPrototype;
22
use Aggrego\Domain\Profile\BoardTransformation\Exception\UnprocessableBoardException;
23
use Aggrego\Domain\Profile\BoardTransformation\Transformation as DomainTransformation;
24
use Assert\Assertion;
25
use Exception;
26
27
class Transformation implements DomainTransformation
28
{
29
    private const KEY_UUID = 'uuid';
30
    private const KEY_NAME = 'name';
31
    private const KEY_VALUE = 'value';
32
33
    /**
34
     * @param Key $key
35
     * @param DataBoard|Board $board
36
     * @return BoardPrototype
37
     * @throws UnprocessableBoardException
38
     */
39
    public function transform(Key $key, Board $board): BoardPrototype
40
    {
41
        if (!$board instanceof DataBoard){
42
            throw new UnprocessableBoardException('Unable to process board due to incorrect board type: ' . get_class($board));
43
        }
44
        $keyValue = $key->getValue();
45
        try {
46
            Assertion::keyExists($keyValue, self::KEY_VALUE);
47
        } catch (Exception $e) {
48
            throw new UnprocessableBoardException('Unable to process board due to: ' . $e->getMessage(), 0, $e);
49
        }
50
        /** @var DataBoard $board */
51
        $key = $this->getKey($keyValue, $board);
52
53
        return new DataBoardPrototype(
54
            $key,
55
            $board->getProfile(),
56
            new Data($keyValue[self::KEY_VALUE])
57
        );
58
    }
59
60
    private function getKey(array $keyValue, DataBoard $board): Key
61
    {
62
        if (isset($keyValue[self::KEY_NAME])) {
63
            $key = new Key(
64
                [
65
                    self::KEY_NAME => $keyValue[self::KEY_NAME],
66
                    self::KEY_UUID => $board->getKey()->getValue()[self::KEY_UUID]
67
                ]
68
            );
69
        } else {
70
            $key = $board->getKey();
71
        }
72
        return $key;
73
    }
74
}
75