OrganizationReference   D
last analyzed

Complexity

Total Complexity 59

Size/Duplication

Total Lines 389
Duplicated Lines 0 %

Test Coverage

Coverage 84.62%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 59
eloc 84
dl 0
loc 389
ccs 110
cts 130
cp 0.8462
rs 4.08
c 1
b 0
f 1

54 Methods

Rating   Name   Duplication   Size   Complexity  
A getEmployeesByRole() 0 3 1
A setEmployees() 0 3 1
A getPermissionsResourceId() 0 3 1
A getDescription() 0 3 1
A setDescription() 0 3 1
A getHydrator() 0 3 1
A setImages() 0 3 1
A hasAssociation() 0 5 1
A __set() 0 3 1
A setHydrator() 0 3 1
A getParent() 0 3 1
A hasProperty() 0 3 1
A getSearchableProperties() 0 3 1
A getTemplate() 0 3 1
A load() 0 28 4
A getImage() 0 3 1
A setId() 0 3 1
A getEmployees() 0 3 1
A getDateCreated() 0 3 1
A getPermissions() 0 3 1
A setParent() 0 3 1
A proxy() 0 11 3
A __get() 0 3 1
A getImages() 0 3 1
A getEmployee() 0 3 1
A getId() 0 3 1
A setImage() 0 3 1
A isOwner() 0 5 1
A setUser() 0 4 1
A setKeywords() 0 3 1
A getOrganizationName() 0 3 1
A getOrganization() 0 5 1
A getUser() 0 3 1
A setDateCreated() 0 3 1
A __isset() 0 3 1
A setWorkflowSettings() 0 3 1
A notEmpty() 0 3 1
A setContact() 0 3 1
A setOrganizationName() 0 3 1
A getWorkflowSettings() 0 3 1
A setTemplate() 0 3 1
A setExternalId() 0 3 1
A getDateModified() 0 3 1
A isEmployee() 0 5 1
A getContact() 0 3 1
A getPermissionsUserIds() 0 3 1
A getExternalId() 0 3 1
A getHiringOrganizations() 0 3 1
A setDateModified() 0 3 1
A __construct() 0 4 1
A clearKeywords() 0 3 1
A getJobs() 0 3 1
A isHiringOrganization() 0 3 1
A setPermissions() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like OrganizationReference often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use OrganizationReference, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright https://yawik.org/COPYRIGHT.php
8
 */
9
10
/** */
11
namespace Organizations\Entity;
12
13
use Auth\Entity\UserInterface;
14
use Core\Entity\EntityInterface;
15
use Core\Entity\MetaDataProviderTrait;
16
use Core\Entity\PermissionsInterface;
17
use Doctrine\Common\Collections\Collection;
18
use Organizations\Repository\Organization as OrganizationRepository;
19
use Laminas\Hydrator\HydratorInterface;
20
21
/**
22
 * Manages reference to an organization.
23
 *
24
 * As OrganizationInterface is also implemented (and all methods are proxied to the "real" organization
25
 * object), this class can be used as an organization.
26
 *
27
 * @author Mathias Gelhausen <[email protected]>
28
 * @todo update test
29
 */
30
class OrganizationReference implements
31
    OrganizationInterface,
32
    OrganizationReferenceInterface
33
{
34
    use MetaDataProviderTrait;
35
36
    /*
37
     * Note: We start property names with an underscore to prevent
38
     * name collisions with the OrganizationInterface.
39
     */
40
41
    /**
42
     * The user id of the parent document.
43
     *
44
     * @var string
45
     */
46
    protected $_userId;
47
48
    /**
49
     * The organization repository.
50
     *
51
     * @var \Organizations\Repository\Organization
52
     */
53
    protected $_repository;
54
55
    /**
56
     * The organization
57
     *
58
     * @var Organization
59
     */
60
    protected $_organization;
61
62
    /**
63
     * Reference type.
64
     *
65
     * @internal
66
     *      Initial value is null, so we can determine in {@link load()} if it has already run once.
67
     *
68
     * @var string
69
     */
70
    protected $_type;
71
72
    /**
73
     * Creates an instance.
74
     *
75
     * @param string                 $userId
76
     * @param OrganizationRepository $repository
77
     */
78 46
    public function __construct($userId, OrganizationRepository $repository)
79
    {
80 46
        $this->_userId = $userId;
81 46
        $this->_repository = $repository;
82
    }
83
84 1
    public function isOwner()
85
    {
86 1
        $this->load();
87
88 1
        return self::TYPE_OWNER == $this->_type;
89
    }
90
91 1
    public function isEmployee()
92
    {
93 1
        $this->load();
94
95 1
        return self::TYPE_EMPLOYEE == $this->_type;
96
    }
97
98 45
    public function hasAssociation()
99
    {
100 45
        $this->load();
101
102 45
        return self::TYPE_NONE != $this->_type;
103
    }
104
105 1
    public function getOrganization()
106
    {
107 1
        $this->load();
108
109 1
        return $this->_organization;
110
    }
111
112
    /**
113
     * Loads the organization from the database and sets the reference type.
114
     */
115 45
    protected function load()
116
    {
117 45
        if (null !== $this->_type) {
118 29
            return;
119
        }
120
121
        // Is the user the owner of the referenced organization?
122 45
        $org = $this->_repository->findByUser($this->_userId);
123
124 45
        if ($org) {
125 23
            $this->_type = self::TYPE_OWNER;
126 23
            $this->_organization = $org;
127
128 23
            return;
129
        }
130
131
        // Is the user employed by the referenced organization?
132 23
        $org = $this->_repository->findByEmployee($this->_userId);
133
134 23
        if ($org) {
135 1
            $this->_type = self::TYPE_EMPLOYEE;
136 1
            $this->_organization = $org;
137
138 1
            return;
139
        }
140
141
        // It seems the user is not associated with an organization.
142 23
        $this->_type = self::TYPE_NONE;
143
    }
144
145
146
    /**
147
     * Executes a proxy call to the associated organization entity.
148
     *
149
     * Does nothing, if no organization is available.
150
     *
151
     * Call it like:
152
     * <pre>
153
     *   $this->proxy($method[, $arg1[, $arg2[, ...]]]);
154
     * </pre>
155
     *
156
     * @param $method
157
     *
158
     * @return self|mixed
159
     */
160 44
    protected function proxy($method)
161
    {
162 44
        if (!$this->hasAssociation()) {
163 22
            return $this;
164
        }
165
166 22
        $args = array_slice(func_get_args(), 1);
167
168 22
        $return = call_user_func_array(array($this->_organization, $method), $args);
169
170 22
        return ($return === $this->_organization) ? $this : $return;
171
    }
172
173
    /*
174
     * We need to implement each method of OrganizationInterface because we want to proxy
175
     * to a concrete instance of Organization and therefor cannot simply extend.
176
     */
177
178
    /**#@+
179
     * Proxies to the concrete Organization instance.
180
     *
181
     * {@inheritDoc}
182
     */
183
184
185
    public function __get($property)
186
    {
187
        return $this->proxy('__get', $property);
188
    }
189
190
    public function __set($property, $value)
191
    {
192
        return $this->proxy('__set', $property, $value);
193
    }
194
195
    public function __isset($property)
196
    {
197
        return $this->proxy('__isset', $property);
198
    }
199
200
    public function notEmpty($property, array $args=[])
201
    {
202
        return $this->proxy('notEmpty', $args);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxy('notEmpty', $args) also could return the type Organizations\Entity\OrganizationReference which is incompatible with the return type mandated by Core\Entity\EntityInterface::notEmpty() of boolean.
Loading history...
203
    }
204
205
    public function hasProperty($property, $mode = self::PROPERTY_STRICT)
206
    {
207
        return $this->proxy('hasProperty', $mode);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxy('hasProperty', $mode) also could return the type Organizations\Entity\OrganizationReference which is incompatible with the return type mandated by Core\Entity\EntityInterface::hasProperty() of boolean.
Loading history...
208
    }
209
210 2
    public function setHydrator(HydratorInterface $hydrator): void
211
    {
212 2
        $this->proxy('setHydrator', $hydrator);
213
    }
214
215 2
    public function getHydrator(): ?HydratorInterface
216
    {
217 2
        return $this->proxy('getHydrator');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxy('getHydrator') could return the type Organizations\Entity\OrganizationReference which is incompatible with the type-hinted return Laminas\Hydrator\HydratorInterface|null. Consider adding an additional type-check to rule them out.
Loading history...
218
    }
219
220 2
    public function setId($id)
221
    {
222 2
        return $this->proxy('setId', $id);
223
    }
224
225 2
    public function getId()
226
    {
227 2
        return $this->proxy('getId');
228
    }
229
230 2
    public function setDateCreated($date)
231
    {
232 2
        return $this->proxy('setDateCreated', $date);
233
    }
234
235 2
    public function getDateCreated()
236
    {
237 2
        return $this->proxy('getDateCreated');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxy('getDateCreated') also could return the type Organizations\Entity\OrganizationReference which is incompatible with the return type mandated by Core\Entity\Modification...rface::getDateCreated() of DateTime|null.
Loading history...
238
    }
239
240 2
    public function setDateModified($date)
241
    {
242 2
        return $this->proxy('setDateModified', $date);
243
    }
244
245 2
    public function getDateModified()
246
    {
247 2
        return $this->proxy('getDateModified');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxy('getDateModified') also could return the type Organizations\Entity\OrganizationReference which is incompatible with the return type mandated by Core\Entity\Modification...face::getDateModified() of DateTime|null.
Loading history...
248
    }
249
250 2
    public function setParent(OrganizationInterface $parent)
251
    {
252 2
        return $this->proxy('setParent', $parent);
253
    }
254
255 2
    public function getParent($returnSelf = false)
256
    {
257 2
        return $this->proxy('getParent', $returnSelf);
258
    }
259
260 2
    public function setContact(EntityInterface $contact = null)
261
    {
262 2
        return $this->proxy('setContact', $contact);
263
    }
264
265 2
    public function getContact()
266
    {
267 2
        return $this->proxy('getContact');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxy('getContact') also could return the type Organizations\Entity\OrganizationReference which is incompatible with the return type mandated by Organizations\Entity\Org...Interface::getContact() of Organizations\Entity\OrganizationContact.
Loading history...
268
    }
269
270 2
    public function isHiringOrganization()
271
    {
272 2
        return $this->proxy('isHiringOrganization');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxy('isHiringOrganization') also could return the type Organizations\Entity\OrganizationReference which is incompatible with the return type mandated by Organizations\Entity\Org...:isHiringOrganization() of boolean.
Loading history...
273
    }
274
275 2
    public function getHiringOrganizations()
276
    {
277 2
        return $this->proxy('getHiringOrganizations');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxy('getHiringOrganizations') also could return the type Organizations\Entity\OrganizationReference which is incompatible with the return type mandated by Organizations\Entity\Org...etHiringOrganizations() of Doctrine\Common\Collections\Collection.
Loading history...
278
    }
279
280 2
    public function setImage(OrganizationImage $image)
281
    {
282 2
        return $this->proxy('setImage', $image);
283
    }
284
285 2
    public function getImage()
286
    {
287 2
        return $this->proxy('getImage');
288
    }
289
290
    public function getImages()
291
    {
292
        return $this->proxy('getImages');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxy('getImages') also could return the type Organizations\Entity\OrganizationReference which is incompatible with the return type mandated by Organizations\Entity\Org...nInterface::getImages() of Core\Entity\ImageSet.
Loading history...
293
    }
294
295
    public function setImages(\Core\Entity\ImageSet $images)
296
    {
297
        return $this->proxy('setImages', $images);
298
    }
299
300 2
    public function setOrganizationName(OrganizationName $organizationNames)
301
    {
302 2
        return $this->proxy('setOrganizationName', $organizationNames);
303
    }
304
305 2
    public function getOrganizationName()
306
    {
307 2
        return $this->proxy('getOrganizationName');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxy('getOrganizationName') also could return the type Organizations\Entity\OrganizationReference which is incompatible with the return type mandated by Organizations\Entity\Org...::getOrganizationName() of Organizations\Entity\OrganizationName.
Loading history...
308
    }
309
310 2
    public function getDescription()
311
    {
312 2
        return $this->proxy('getDescription');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxy('getDescription') also could return the type Organizations\Entity\OrganizationReference which is incompatible with the return type mandated by Organizations\Entity\Org...rface::getDescription() of string.
Loading history...
313
    }
314
315 2
    public function setDescription($description)
316
    {
317 2
        return $this->proxy('setDescription', $description);
318
    }
319
320 2
    public function setEmployees(Collection $employees)
321
    {
322 2
        return $this->proxy('setEmployees', $employees);
323
    }
324
325 2
    public function getEmployees()
326
    {
327 2
        return $this->proxy('getEmployees');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxy('getEmployees') also could return the type Organizations\Entity\OrganizationReference which is incompatible with the return type mandated by Organizations\Entity\Org...terface::getEmployees() of Doctrine\Common\Collections\Collection.
Loading history...
328
    }
329
330
    public function getEmployeesByRole($role)
331
    {
332
        return $this->proxy('getEmployeesByRole', $role);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxy('getEmployeesByRole', $role) also could return the type Organizations\Entity\OrganizationReference which is incompatible with the return type mandated by Organizations\Entity\Org...e::getEmployeesByRole() of Doctrine\Common\Collections\Collection.
Loading history...
333
    }
334
335 2
    public function getEmployee($userOrId)
336
    {
337 2
        return $this->proxy('getEmployee', $userOrId);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxy('getEmployee', $userOrId) also could return the type Organizations\Entity\OrganizationReference which is incompatible with the return type mandated by Organizations\Entity\Org...nterface::getEmployee() of Organizations\Entity\EmployeeInterface|null.
Loading history...
338
    }
339
340 2
    public function setExternalId($externalId)
341
    {
342 2
        return $this->proxy('setExternalId', $externalId);
343
    }
344
345 2
    public function getExternalId()
346
    {
347 2
        return $this->proxy('getExternalId');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxy('getExternalId') also could return the type Organizations\Entity\OrganizationReference which is incompatible with the return type mandated by Organizations\Entity\Org...erface::getExternalId() of string.
Loading history...
348
    }
349
350 2
    public function getUser()
351
    {
352 2
        return $this->proxy('getUser');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxy('getUser') also could return the type Organizations\Entity\OrganizationReference which is incompatible with the return type mandated by Organizations\Entity\Org...ionInterface::getUser() of Auth\Entity\UserInterface.
Loading history...
353
    }
354
355 2
    public function setUser(UserInterface $user)
356
    {
357 2
        $this->_type = null; // force reload of references!
358 2
        return $this->proxy('setUser', $user);
359
    }
360
361 2
    public function getJobs()
362
    {
363 2
        return $this->proxy('getJobs');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxy('getJobs') also could return the type Organizations\Entity\OrganizationReference which is incompatible with the return type mandated by Organizations\Entity\Org...ionInterface::getJobs() of Doctrine\Common\Collections\Collection.
Loading history...
364
    }
365
366 2
    public function getPermissions()
367
    {
368 2
        return $this->proxy('getPermissions');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxy('getPermissions') also could return the type Organizations\Entity\OrganizationReference which is incompatible with the return type mandated by Core\Entity\PermissionsA...rface::getPermissions() of Core\Entity\PermissionsInterface.
Loading history...
369
    }
370
371 2
    public function setPermissions(PermissionsInterface $permissions)
372
    {
373 2
        return $this->proxy('setPermissions', $permissions);
374
    }
375
376 2
    public function getPermissionsResourceId()
377
    {
378 2
        return $this->proxy('getPermissionsResourceId');
379
    }
380
381 2
    public function getPermissionsUserIds($type = null)
382
    {
383 2
        return $this->proxy('getPermissionsUSerIds', $type);
384
    }
385
386 2
    public function getSearchableProperties()
387
    {
388 2
        return $this->proxy('getSearchableProperties');
389
    }
390
391
    public function setKeywords(array $keywords)
392
    {
393
        return $this->proxy('setKeywords', $keywords);
394
    }
395
396
    public function clearKeywords()
397
    {
398
        return $this->proxy('clearKeywords');
399
    }
400
401 2
    public function getTemplate()
402
    {
403 2
        return $this->proxy('getTemplate');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxy('getTemplate') also could return the type Organizations\Entity\OrganizationReference which is incompatible with the return type mandated by Organizations\Entity\Org...nterface::getTemplate() of Organizations\Entity\TemplateInterface.
Loading history...
404
    }
405
406 2
    public function setTemplate(TemplateInterface $template)
407
    {
408 2
        return $this->proxy('setTemplate', $template);
409
    }
410
411 2
    public function getWorkflowSettings()
412
    {
413 2
        return $this->proxy('getWorkflowSettings');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->proxy('getWorkflowSettings') also could return the type Organizations\Entity\OrganizationReference which is incompatible with the return type mandated by Organizations\Entity\Org...::getWorkflowSettings() of Organizations\Entity\WorkflowSettingsInterface.
Loading history...
414
    }
415
416 2
    public function setWorkflowSettings($workflowSettings)
417
    {
418 2
        return $this->proxy('setWorkflowSettings', $workflowSettings);
419
    }
420
421
    /**#@-*/
422
}
423