Passed
Pull Request — master (#154)
by Alex
07:05
created

EntityGubbins   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 35
c 0
b 0
f 0
dl 0
loc 224
rs 9

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getOdataResourceType() 0 3 1
A getStubs() 0 3 1
A getFieldNames() 0 7 2
A resolveAssociation() 0 4 3
A getName() 0 3 1
A setOdataResourceType() 0 7 2
A isOK() 0 6 1
B setFields() 0 22 6
A getKeyFields() 0 3 1
A setStubs() 0 9 3
B addAssociation() 0 11 6
A setClassName() 0 3 1
A getClassName() 0 3 1
A getFields() 0 3 1
A setName() 0 3 1
A getAssociations() 0 3 1
A getAssociationNames() 0 10 3
1
<?php
2
3
namespace AlgoWeb\PODataLaravel\Models\ObjectMap\Entities;
4
5
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\Association;
6
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationMonomorphic;
7
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationStubBase;
8
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationStubMonomorphic;
9
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationStubPolymorphic;
10
use POData\Providers\Metadata\ResourceEntityType;
11
12
class EntityGubbins
13
{
14
    /**
15
     * @var string
16
     */
17
    private $name;
18
19
    /**
20
     * @var string
21
     */
22
    private $className;
23
24
    /**
25
     * @var EntityField[]
26
     */
27
    private $keyFields = [];
28
29
    /**
30
     * @var EntityField[]
31
     */
32
    private $fields = [];
33
34
    /**
35
     * @var AssociationStubBase[]
36
     */
37
    private $stubs = [];
38
39
    /**
40
     * @var Association[]
41
     */
42
    private $associations = [];
43
    /**
44
     * @var ResourceEntityType
45
     */
46
    private $odataResourceType;
47
48
    /**
49
     * @return ResourceEntityType
50
     */
51
    public function getOdataResourceType()
52
    {
53
        return $this->odataResourceType;
54
    }
55
56
    /**
57
     * @param ResourceEntityType $odataType
58
     */
59
    public function setOdataResourceType(ResourceEntityType $odataType)
60
    {
61
        if ($odataType->isAbstract()) {
62
            $msg = 'OData resource entity type must be concrete';
63
            throw new \InvalidArgumentException($msg);
64
        }
65
        $this->odataResourceType = $odataType;
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function getName()
72
    {
73
        return $this->name;
74
    }
75
76
    /**
77
     * @param string $name
78
     */
79
    public function setName($name)
80
    {
81
        $this->name = $name;
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getClassName()
88
    {
89
        return $this->className;
90
    }
91
92
    /**
93
     * @param string $className
94
     */
95
    public function setClassName($className)
96
    {
97
        $this->className = $className;
98
    }
99
100
    /**
101
     * @return EntityField[]
102
     */
103
    public function getKeyFields()
104
    {
105
        return $this->keyFields;
106
    }
107
108
    /**
109
     * @return EntityField[]
110
     */
111
    public function getFields()
112
    {
113
        return $this->fields;
114
    }
115
116
    /**
117
     * @param EntityField[] $fields
118
     */
119
    public function setFields(array $fields)
120
    {
121
        if (0 == count($fields)) {
122
            $msg = 'Fields array must not be empty';
123
            throw new \Exception($msg);
124
        }
125
        $keys = [];
126
        foreach ($fields as $propName => $field) {
127
            if (!$field instanceof EntityField) {
128
                $msg = 'Fields array must only have EntityField objects';
129
                throw new \Exception($msg);
130
            }
131
            if ($field->getIsKeyField()) {
132
                $keys[$propName] = $field;
133
            }
134
        }
135
        if (0 == count($keys)) {
136
            $msg = 'No key field supplied in fields array';
137
            throw new \Exception($msg);
138
        }
139
        $this->fields = $fields;
140
        $this->keyFields = $keys;
141
    }
142
143
    /**
144
     * @return AssociationStubBase[]
145
     */
146
    public function getStubs()
147
    {
148
        return $this->stubs;
149
    }
150
151
    /**
152
     * @param AssociationStubBase[] $stubs
153
     */
154
    public function setStubs(array $stubs)
155
    {
156
        foreach ($stubs as $field) {
157
            if (!$field instanceof AssociationStubBase) {
158
                $msg = 'Stubs array must only have AssociationStubBase objects';
159
                throw new \Exception($msg);
160
            }
161
        }
162
        $this->stubs = $stubs;
163
    }
164
165
    /**
166
     * @param Association $association
167
     * @param bool        $isFirst
168
     */
169
    public function addAssociation(Association $association, $isFirst = true)
170
    {
171
        if ($association instanceof AssociationMonomorphic) {
172
            $stub = $isFirst ? $association->getFirst() : $association->getLast();
173
            if (null === $stub || (!in_array($stub, $this->stubs) && !($stub instanceof AssociationStubPolymorphic))) {
174
                throw new \InvalidArgumentException('Association cannot be connected to this entity');
175
            }
176
            $propertyName = $stub->getRelationName();
177
        }
178
        assert(isset($propertyName));
0 ignored issues
show
Bug introduced by
The call to assert() has too few arguments starting with description. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

178
        /** @scrutinizer ignore-call */ 
179
        assert(isset($propertyName));

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
179
        $this->associations[$propertyName] = $association;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $propertyName does not seem to be defined for all execution paths leading up to this point.
Loading history...
180
    }
181
182
    /**
183
     * @return String[]
184
     */
185
    protected function getFieldNames()
186
    {
187
        $fieldNames = [];
188
        foreach ($this->fields as $field) {
189
            $fieldNames[] = $field->getName();
190
        }
191
        return $fieldNames;
192
    }
193
194
    /**
195
     * @return String[]
196
     */
197
    protected function getAssociationNames()
198
    {
199
        if (empty($this->stubs)) {
200
            return [];
201
        }
202
        $assocationNames = [];
203
        foreach ($this->stubs as $stub) {
204
            $assocationNames[] = $stub->getRelationName();
205
        }
206
        return $assocationNames;
207
    }
208
209
    /**
210
     * @return Associations\Association[]
211
     */
212
    public function getAssociations()
213
    {
214
        return $this->associations;
215
    }
216
217
    /**
218
     * @param $relName
219
     * @return Association|null
220
     */
221
    public function resolveAssociation($relName)
222
    {
223
        $this->associations = is_array($this->associations) ? $this->associations : [];
224
        return array_key_exists($relName, $this->associations) ? $this->associations[$relName] : null;
225
    }
226
227
    /**
228
     * @return bool
229
     */
230
    public function isOK()
231
    {
232
        $fieldNames = $this->getFieldNames();
233
        $associationNames = $this->getAssociationNames();
234
        $intersection = array_intersect($fieldNames, $associationNames);
235
        return 0 === count($intersection);
236
    }
237
}
238