Map   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
eloc 28
c 1
b 0
f 0
dl 0
loc 105
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A addEntity() 0 3 1
A getEntities() 0 3 1
A setAssociations() 0 4 2
A getAssociations() 0 3 1
A isOK() 0 8 3
A addAssociation() 0 8 2
A setEntities() 0 10 4
A addAssociationMonomorphic() 0 6 1
A resolveEntity() 0 3 2
1
<?php
2
3
declare(strict_types=1);
4
namespace AlgoWeb\PODataLaravel\Models\ObjectMap;
5
6
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\Association;
7
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationMonomorphic;
8
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\EntityGubbins;
9
10
class Map
11
{
12
    /**
13
     * @var EntityGubbins[]
14
     */
15
    private $entities = [];
16
17
    /**
18
     * @var Association[]
19
     */
20
    private $associations = [];
21
22
    /**
23
     * @param EntityGubbins $entity
24
     */
25
    public function addEntity(EntityGubbins $entity): void
26
    {
27
        $this->entities[$entity->getClassName()] = $entity;
28
    }
29
30
    /**
31
     * @return EntityGubbins[]
32
     */
33
    public function getEntities()
34
    {
35
        return $this->entities;
36
    }
37
38
    public function resolveEntity(string $entityClassName): ?EntityGubbins
39
    {
40
        return array_key_exists($entityClassName, $this->entities) ? $this->entities[$entityClassName] : null;
41
    }
42
43
    /**
44
     * @param EntityGubbins[] $entities
45
     */
46
    public function setEntities(array $entities): void
47
    {
48
        $this->entities = [];
49
        foreach ($entities as $entity) {
50
            if (!$entity instanceof EntityGubbins) {
51
                throw new \InvalidArgumentException('Entities array must contain only EntityGubbins objects');
52
            }
53
        }
54
        foreach ($entities as $entity) {
55
            $this->entities[$entity->getClassName()] = $entity;
56
        }
57
    }
58
59
    /**
60
     * @param  Association[]                            $associations
61
     * @throws \POData\Common\InvalidOperationException
62
     */
63
    public function setAssociations(array $associations): void
64
    {
65
        foreach ($associations as $association) {
66
            $this->addAssociation($association);
67
        }
68
    }
69
70
    /**
71
     * @param  Association                              $association
72
     * @throws \POData\Common\InvalidOperationException
73
     */
74
    public function addAssociation(Association $association): void
75
    {
76
        if ($association instanceof AssociationMonomorphic) {
77
            $this->addAssociationMonomorphic($association);
78
        } else {
79
            throw new \InvalidArgumentException('Association type not yet handled');
80
        }
81
        $this->associations[] = $association;
82
    }
83
84
    /**
85
     * @return Association[]
86
     */
87
    public function getAssociations(): array
88
    {
89
        return $this->associations;
90
    }
91
92
    /**
93
     * @return bool
94
     */
95
    public function isOK(): bool
96
    {
97
        foreach ($this->entities as $entity) {
98
            if (!$entity->isOK()) {
99
                return false;
100
            }
101
        }
102
        return true;
103
    }
104
105
    /**
106
     * @param  AssociationMonomorphic                   $association
107
     * @throws \POData\Common\InvalidOperationException
108
     */
109
    private function addAssociationMonomorphic(AssociationMonomorphic $association): void
110
    {
111
        $firstClass  = $this->entities[$association->getFirst()->getBaseType()];
112
        $secondClass = $this->entities[$association->getLast()->getBaseType()];
113
        $firstClass->addAssociation($association);
114
        $secondClass->addAssociation($association, false);
115
    }
116
}
117