Passed
Pull Request — 1.x (#13)
by
unknown
41:33 queued 26:35
created

RelationMapper   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 3
eloc 18
c 2
b 1
f 0
dl 0
loc 39
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A mapWithNode() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Schema\Renderer\MermaidRenderer;
6
7
use Cycle\ORM\Relation;
8
use Cycle\Schema\Renderer\MermaidRenderer\Exception\RelationNotFoundException;
9
10
final class RelationMapper
11
{
12
    private const RELATION_TYPE = 1;
13
14
    private const RELATIONS = [
15
        Relation::HAS_ONE => ['HO', '-->'],
16
        Relation::HAS_MANY => ['HM', '--o'],
17
        Relation::BELONGS_TO => ['BT', '-->'],
18
        Relation::MANY_TO_MANY => ['MtM', '--*'],
19
        Relation::REFERS_TO => ['RT', '-->'],
20
        Relation::MORPHED_HAS_MANY => ['MoHM', '--o'],
21
        Relation::MORPHED_HAS_ONE => ['MoHO', '-->'],
22
        Relation::BELONGS_TO_MORPHED => ['BtM', '-->'],
23
        Relation::EMBEDDED => ['Emb', '--'],
24
    ];
25
26
    /**
27
     * Map relation name
28
     *
29
     * @param int $code
30
     * @param bool $isNullable
31
     *
32
     * @throws RelationNotFoundException
33
     *
34
     * @return array
35
     */
36
    public function mapWithNode(int $code, bool $isNullable = false): array
37
    {
38
        if (!isset(self::RELATIONS[$code])) {
39
            throw new RelationNotFoundException();
40
        }
41
42
        $relation = self::RELATIONS[$code];
43
44
        if ($isNullable) {
45
            $relation[self::RELATION_TYPE] = $relation[self::RELATION_TYPE] . ' "nullable"';
46
        }
47
48
        return $relation;
49
    }
50
}
51