Organization   F
last analyzed

Complexity

Total Complexity 84

Size/Duplication

Total Lines 812
Duplicated Lines 0 %

Test Coverage

Coverage 89.16%

Importance

Changes 0
Metric Value
wmc 84
eloc 161
dl 0
loc 812
ccs 148
cts 166
cp 0.8916
rs 2
c 0
b 0
f 0

46 Methods

Rating   Name   Duplication   Size   Complexity  
A getParent() 0 3 3
A getEmployee() 0 12 4
A setImage() 0 5 1
B getPermissionsUserIds() 0 39 11
A setWorkflowSettings() 0 5 1
A getContact() 0 7 2
A setPermissions() 0 9 2
A getImage() 0 7 4
A getUser() 0 3 1
A getPermissions() 0 11 3
A getProfileSetting() 0 3 1
A setOrganizationName() 0 10 2
A setUser() 0 9 2
A getPermissionsResourceId() 0 3 1
A getEmployees() 0 12 3
A getResourceId() 0 3 1
A setParent() 0 5 1
A getWorkflowSettings() 0 7 2
A isHiringOrganization() 0 3 1
A getDescription() 0 3 1
A __construct() 0 3 1
A setContact() 0 8 2
A getOrganizationName() 0 3 1
A getName() 0 7 2
A removeImages() 0 5 1
A getTemplate() 0 7 2
A getHydrator() 0 3 1
A getExternalId() 0 3 1
A setProfileSetting() 0 5 1
A getImages() 0 6 2
A getSearchableProperties() 0 3 1
A isOwner() 0 3 1
A isEmployee() 0 3 2
A getEmployeesByRole() 0 12 3
A setEmployees() 0 8 2
A setIsDraft() 0 5 1
A getJobs() 0 3 1
A isAssociated() 0 3 2
A getHiringOrganizations() 0 3 1
A setExternalId() 0 5 1
A setTemplate() 0 5 1
A setHydrator() 0 2 1
A setDescription() 0 5 1
A isDraft() 0 3 1
A setImages() 0 5 1
A updatePermissions() 0 21 3

How to fix   Complexity   

Complex Class

Complex classes like Organization 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 Organization, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @copyright https://yawik.org/COPYRIGHT.php
6
 * @license       MIT
7
 */
8
9
namespace Organizations\Entity;
10
11
use Auth\Entity\UserInterface;
12
use Core\Entity\AbstractIdentifiableModificationDateAwareEntity as BaseEntity;
13
use Core\Entity\Collection\ArrayCollection;
14
use Core\Entity\DraftableEntityInterface;
15
use Core\Entity\EntityInterface;
16
use Core\Entity\Hydrator\EntityHydrator;
17
use Core\Entity\ImageInterface;
18
use Core\Entity\ImageSet;
19
use Core\Entity\ImageSetInterface;
20
use Core\Entity\MetaDataProviderTrait;
21
use Core\Entity\Permissions;
22
use Core\Entity\PermissionsInterface;
23
use Doctrine\Common\Collections\Collection;
24
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
25
use Laminas\Hydrator\HydratorInterface;
26
use Laminas\Permissions\Acl\Resource\ResourceInterface;
27
28
/**
29
 * The organization.
30
 *
31
 * @ODM\Document(collection="organizations", repositoryClass="Organizations\Repository\Organization")
32
 * @ODM\HasLifecycleCallbacks
33
 * @ODM\Indexes({
34
 *      @ODM\Index(keys={
35
 *          "_organizationName"="text"
36
 *      }, name="fulltext")
37
 * })
38
 ** @author Mathias Weitz <[email protected]>
39
 * @author Mathias Gelhausen <[email protected]>
40
 * @author Anthonius Munthi <[email protected]>
41
 */
42
class Organization extends BaseEntity implements
43
    OrganizationInterface,
44
    DraftableEntityInterface,
45
    ResourceInterface
46
{
47
    use MetaDataProviderTrait;
48
49
    /**
50
     * Always enabled even if there are no active jobs
51
     */
52
    const PROFILE_ALWAYS_ENABLE     = 'always';
53
54
    /**
55
     * Hide if there are no jobs available
56
     */
57
    const PROFILE_ACTIVE_JOBS       = 'active-jobs';
58
59
    /**
60
     * Always disabled profile
61
     */
62
    const PROFILE_DISABLED          = 'disabled';
63
64
    /**
65
     * Event name of post construct event.
66
     *
67
     * @var string
68
     */
69
    const postConstruct = 'postRepositoryConstruct';
70
71
    /**
72
     * externalId. Allows external applications to reference their primary key.
73
     *
74
     * @var string
75
     * @ODM\Field(type="string")
76
     * @ODM\Index
77
     */
78
    protected $externalId;
79
80
    /**
81
     * The actual name of the organization.
82
     *
83
     * @var \Organizations\Entity\OrganizationName
84
     * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\OrganizationName", storeAs="id", cascade="persist")
85
     */
86
    protected $organizationName;
87
88
    /**
89
     * Only for sorting/searching purposes
90
     *
91
     * @var string
92
     * @ODM\Field(type="string")
93
     */
94
    protected $_organizationName;
95
96
    /**
97
     * Assigned permissions.
98
     *
99
     * @var PermissionsInterface
100
     * @ODM\EmbedOne(targetDocument="\Core\Entity\Permissions")
101
     */
102
    protected $permissions;
103
104
    /**
105
     * primary logo of an organization
106
     *
107
     * @var \Organizations\Entity\OrganizationImage
108
     * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\OrganizationImage", inversedBy="organization", storeAs="id", nullable="true", cascade={"all"})
109
     */
110
    protected ?OrganizationImage $image = null;
111
112
    /**
113
     * @ODM\EmbedOne(targetDocument="Core\Entity\ImageSet")
114
     */
115
    protected ?ImageSetInterface $images = null;
116
117
    /**
118
     * Flag indicating draft state of this job.
119
     *
120
     * @var bool
121
     * @ODM\Field(type="bool")
122
     */
123
    protected $isDraft = false;
124
125
    /**
126
     * Organization contact data.
127
     *
128
     * @ODM\EmbedOne(targetDocument="\Organizations\Entity\OrganizationContact")
129
     */
130
    protected $contact;
131
132
    /**
133
     * The organizations' description.
134
     *
135
     * @var string
136
     * @ODM\Field(type="string")
137
     */
138
    protected $description;
139
140
    /**
141
     * The parent of this organization.
142
     *
143
     * @see   setParent()
144
     * @var OrganizationInterface | null
145
     * @ODM\ReferenceOne(targetDocument="\Organizations\Entity\Organization", storeAs="id", nullable=true)
146
     * @since 0.18
147
     */
148
    protected $parent;
149
150
    /**
151
     * The hiring organizations of this organization.
152
     *
153
     * @var Collection
154
     * @ODM\ReferenceMany(
155
     *      targetDocument="Organizations\Entity\Organization",
156
     *      repositoryMethod="getHiringOrganizationsCursor"
157
     * )
158
     * @since 0.18
159
     */
160
    protected $hiringOrganizations;
161
162
    /**
163
     * The associated employees (users)
164
     *
165
     * @ODM\EmbedMany(targetDocument="\Organizations\Entity\Employee")
166
     * @var Collection
167
     */
168
    protected $employees;
169
170
    /**
171
     * Jobs of this organization.
172
     *
173
     * @var Collection
174
     * @ODM\ReferenceMany(targetDocument="\Jobs\Entity\Job", storeAs="id", mappedBy="organization")
175
     * @since 0.18
176
     */
177
    protected $jobs;
178
179
    /**
180
     * the owner of a Organization
181
     *
182
     * @var UserInterface $user
183
     * @ODM\ReferenceOne(targetDocument="\Auth\Entity\User", storeAs="id", cascade={"all"})
184
     * @ODM\Index
185
     */
186
    protected $user;
187
188
    /**
189
     * Default values of an organizations job template
190
     *
191
     * @var TemplateInterface;
192
     * @ODM\EmbedOne(targetDocument="\Organizations\Entity\Template")
193
     */
194
    protected $template;
195
196
    /**
197
     * Default values Workflow
198
     *
199
     * @var WorkflowSettingsInterface $workflowSettings ;
200
     * @ODM\EmbedOne(targetDocument="\Organizations\Entity\WorkflowSettings")
201
     */
202
    protected $workflowSettings;
203
204
    /**
205
     * Profile Setting
206
     * @var string
207
     * @ODM\Field(type="string", nullable=true)
208
     */
209
    protected $profileSetting;
210
211
    public function __construct()
212
    {
213
        $this->images = new ImageSet();
214
    }
215
216
    /**
217 1
     * Sets the logo of an organization
218
     *
219 1
     * @param OrganizationImage $image
220
     *
221
     * @return self
222
     * @deprecated since 0.29; use $this->getImages()->set()
223
     */
224
    public function setImage(OrganizationImage $image = null)
225
    {
226
        $this->image = $image;
227 1
228
        return $this;
229 1
    }
230
231 1
    /**
232
     * @param string|bool $key
233
     * @return OrganizationImage|ImageInterface|null
234
     */
235
    public function getImage($key = ImageSet::ORIGINAL): ?OrganizationImage
236
    {
237
        if (is_bool($key)) {
238
            $key = $key ? ImageSet::THUMBNAIL : ImageSet::ORIGINAL;
239
        }
240
241
        return $this->getImages()->get($key, false) ?: $this->image;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getImages(... false) ?: $this->image could return the type Core\Entity\ImageInterface which includes types incompatible with the type-hinted return Organizations\Entity\OrganizationImage|null. Consider adding an additional type-check to rule them out.
Loading history...
242
    }
243
244
    /**
245
     * @return string
246
     */
247
    public function getProfileSetting()
248
    {
249 2
        return $this->profileSetting;
250
    }
251 2
252 1
    /**
253
     * @param string $profileSetting
254
     *
255 1
     * @return $this
256
     */
257
    public function setProfileSetting($profileSetting)
258
    {
259
        $this->profileSetting = $profileSetting;
260
261
        return $this;
262
    }
263
264
    /**
265 2
     * Returns the string identifier of the Resource
266
     *
267 2
     * @return string
268
     */
269 2
    public function getResourceId()
270
    {
271
        return 'Entity/Organization';
272
    }
273
274
    /**
275
     * Gets the organization name
276
     *
277
     * @return string
278
     */
279
    public function getName()
280
    {
281
        if (empty($this->organizationName)) {
282
            return '';
283
        }
284
285
        return $this->organizationName->getName();
286
    }
287
288 1
    /**
289
     * Sets the parent of an organization
290 1
     *
291
     * @param OrganizationInterface $parent
292
     *
293
     * @return $this
294
     */
295
    public function setParent(OrganizationInterface $parent)
296
    {
297
        $this->parent = $parent;
298 1
299
        return $this;
300 1
    }
301
302
    /**
303
     * @deprecated
304
     * @return array
305
     */
306
    public function getSearchableProperties()
307
    {
308 1
        return array();
309
    }
310 1
311
    /**
312
     * Gets the parent of an organization
313
     *
314
     * @param bool $returnSelf returns itself, if this organization does not have a parent?
315
     *
316
     * @return null|OrganizationInterface
317
     */
318
    public function getParent($returnSelf = false)
319
    {
320 1
        return $this->parent ? : ($returnSelf ? $this : null);
321
    }
322 1
323
    /**
324 1
     * Gets the Draft flag
325
     *
326
     * @return bool
327
     */
328
    public function isDraft()
329
    {
330 5
        return $this->isDraft;
331
    }
332 5
333
    /**
334
     * Gets linked organizations
335
     *
336
     * @return Collection
337
     */
338
    public function getHiringOrganizations()
339
    {
340
        return $this->hiringOrganizations;
341
    }
342
343
    /**
344
     * Sets the draft flag
345
     *
346
     * @param bool $flag
347
     *
348
     * @return $this
349
     */
350
    public function setIsDraft($flag)
351
    {
352
        $this->isDraft = (bool) $flag;
353
354 1
        return $this;
355
    }
356 1
357
    /**
358 1
     * @return bool
359
     */
360
    public function isHiringOrganization()
361
    {
362
        return null !== $this->parent;
363
    }
364
365
    /**
366
     * Returns true, if the user belongs to the organization.
367
     *
368
     * @param UserInterface $user
369
     *
370
     * @return bool
371
     */
372
    public function isAssociated(UserInterface $user)
373
    {
374
        return $this->isOwner($user) || $this->isEmployee($user);
375
    }
376
377
    /**
378 1
     * Sets the external id.
379
     *
380 1
     * @param $externalId
381
     *
382
     * @return self
383
     */
384
    public function setExternalId($externalId)
385
    {
386
        $this->externalId = $externalId;
387
388
        return $this;
389
    }
390
391
    /**
392
     * Checks, if a User is the owner of an organization
393
     *
394
     * @param UserInterface $user
395
     *
396
     * @return bool
397
     */
398
    public function isOwner(UserInterface $user)
399
    {
400
        return $this->getUser()->getId() == $user->getId();
401
    }
402
403
    /**
404
     * Gets the internal id.
405
     *
406
     * @return string
407
     */
408
    public function getExternalId()
409
    {
410
        return $this->externalId;
411
    }
412
413
    /**
414
     * Returns true, if a User is an employee of the organization
415
     *
416
     * @param UserInterface $user
417 1
     *
418
     * @return bool
419 1
     */
420
    public function isEmployee(UserInterface $user)
421
    {
422
        return $this->refs && in_array($user->getId(), $this->refs->getEmployeeIds());
0 ignored issues
show
Bug Best Practice introduced by
The property refs does not exist on Organizations\Entity\Organization. Since you implemented __get, consider adding a @property annotation.
Loading history...
423
    }
424
425 1
    /**
426
     * @todo review this
427
     *
428
     * @param HydratorInterface $hydrator
429 1
     *
430
     * @return $this
431 1
     */
432
    public function setHydrator(HydratorInterface $hydrator): void
433 1
    {
434
435 1
    }
436
437 1
    /**
438
     * Updates the organizationsPermissions to allow all employees to view this organization.
439
     *
440
     * In case of a HiringOrganization Permissions are granted to all employees of the parent
441
     * organization.
442
     *
443
     * @ODM\PreUpdate
444 1
     * @ODM\PrePersist
445
     * @since 0.18
446 1
     */
447
    public function updatePermissions()
448
    {
449
        if ($this->isHiringOrganization()) {
450
            $organization = $this->getParent();
451
            $owner        = $organization->getUser();
452
453
            $this->setUser($owner);
454
        } else {
455
            $organization = $this;
456 4
        }
457
458 4
        /* @var $employees null | ArrayCollection | \Doctrine\ODM\MongoDB\PersistentCollection */
459 1
        $employees = $organization->getEmployees();
460
461 4
        $perms = $this->getPermissions();
462 4
463 4
        foreach ($employees as $emp) {
464
            /* @var $emp \Organizations\Entity\Employee */
465 4
            $perms->grant($emp->getUser(), PermissionsInterface::PERMISSION_CHANGE, false);
0 ignored issues
show
Unused Code introduced by
The call to Core\Entity\PermissionsInterface::grant() has too many arguments starting with false. ( Ignorable by Annotation )

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

465
            $perms->/** @scrutinizer ignore-call */ 
466
                    grant($emp->getUser(), PermissionsInterface::PERMISSION_CHANGE, false);

This check compares calls to functions or methods with their respective definitions. If the call has more 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...
466
        }
467
        $perms->build();
0 ignored issues
show
Bug introduced by
The method build() does not exist on Core\Entity\PermissionsInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Core\Entity\PermissionsInterface. ( Ignorable by Annotation )

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

467
        $perms->/** @scrutinizer ignore-call */ 
468
                build();
Loading history...
468
    }
469
470
    /**
471
     * * @todo review this
472
     * @return EntityHydrator
473 2
     */
474
    public function getHydrator(): ?HydratorInterface
475 2
    {
476
        return new EntityHydrator();
477
    }
478
479
    /**
480
     * Sets the name of an organization
481
     *
482
     * @param OrganizationName $organizationName
483 4
     *
484
     * @return $this
485 4
     */
486 2
    public function setOrganizationName(OrganizationName $organizationName)
487 2
    {
488 2
        if (isset($this->organizationName)) {
489
            $this->organizationName->refCounterDec()->refCompanyCounterDec();
490 2
        }
491
        $this->organizationName = $organizationName;
492
        $this->organizationName->refCounterInc()->refCompanyCounterInc();
493 4
        $this->_organizationName = $organizationName->getName();
494
495
        return $this;
496
    }
497
498
    /**
499
     * Gets the organization name entity of an organisation
500
     *
501
     * @return OrganizationName
502
     */
503 4
    public function getOrganizationName()
504
    {
505
        return $this->organizationName;
506 4
    }
507 2
508
    /**
509 4
     * Gets the Permissions of an organization
510
     *
511 4
     * @return PermissionsInterface
512
     */
513
    public function getPermissions()
514
    {
515
        if (!$this->permissions) {
516
            $permissions = new Permissions();
517
            if ($this->user) {
518
                $permissions->grant($this->user, Permissions::PERMISSION_ALL);
519 2
            }
520
            $this->setPermissions($permissions);
521 2
        }
522
523
        return $this->permissions;
524
    }
525
526
    /**
527
     * Sets the Permissions of an Organization
528
     *
529 2
     * @param PermissionsInterface $permissions
530
     *
531
     * @return $this
532 2
     */
533 2
    public function setPermissions(PermissionsInterface $permissions)
534 1
    {
535 2
        // Assure the user has always all rights.
536
        if ($this->user) {
537 2
            $permissions->grant($this->user, Permissions::PERMISSION_ALL);
538 1
        }
539
        $this->permissions = $permissions;
540
541 2
        return $this;
542 2
    }
543 2
544
    /**
545 1
     * Gets the Permissions Resource ID
546 1
     *
547
     * @return string
548
     */
549 2
    public function getPermissionsResourceId()
550
    {
551 2
        return 'organization:' . $this->getId();
552
    }
553 2
554
    /**
555 1
     * @param null $type
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $type is correct as it would always require null to be passed?
Loading history...
556 1
     *
557
     * @return array
558
     */
559 1
    public function getPermissionsUserIds($type = null)
560 1
    {
561 1
        // if we have a user, grant him full access to all associated permissions.
562 1
        $user = $this->getUser();
563 1
        $spec = $user
0 ignored issues
show
introduced by
$user is of type Auth\Entity\UserInterface, thus it always evaluated to true.
Loading history...
564
            ? $spec = array(PermissionsInterface::PERMISSION_ALL => array($this->getUser()->getId()))
0 ignored issues
show
Unused Code introduced by
The assignment to $spec is dead and can be removed.
Loading history...
565
            : array();
566
567 2
        if (null === $type || ('Job/Permissions' != $type && 'Application' != $type)) {
0 ignored issues
show
introduced by
The condition null === $type is always true.
Loading history...
568
            return $spec;
569
        }
570
571
        if ('Job/Permissions' == $type) {
572
            $change = EmployeePermissionsInterface::JOBS_CHANGE;
573
            $view   = EmployeePermissionsInterface::JOBS_VIEW;
574
        } else {
575
            $change = EmployeePermissionsInterface::APPLICATIONS_CHANGE;
576
            $view   = EmployeePermissionsInterface::APPLICATIONS_VIEW;
577
        }
578
579
        $employees = $this->isHiringOrganization()
580
            ? $this->getParent()->getEmployees()
581
            : $this->getEmployees();
582
583
        foreach ($employees as $emp) {
584
            /* @var $emp EmployeeInterface */
585
            if ($emp->isUnassigned()) {
586
                continue;
587
            }
588
589
            $perm = $emp->getPermissions();
590
            if ($perm->isAllowed($change)) {
591
                $spec[PermissionsInterface::PERMISSION_CHANGE][] = $emp->getUser()->getId();
592
            } elseif ($perm->isAllowed($view)) {
593
                $spec[PermissionsInterface::PERMISSION_VIEW][] = $emp->getUser()->getId();
594
            }
595
        }
596
597
        return $spec;
598
    }
599
600
    /**
601
     * @param ImageSetInterface $images
602
     *
603
     * @return self
604
     */
605
    public function setImages(ImageSetInterface $images)
606
    {
607
        $this->images = $images;
608
609
        return $this;
610 2
    }
611
612 2
    /**
613
     * @return ImageSetInterface
614 2
     */
615
    public function getImages(): ?ImageSetInterface
616
    {
617
        if(is_null($this->images)){
618
            $this->images = new ImageSet();
619
        }
620 3
        return $this->images;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->images also could return the type Core\Entity\ImageSetInterface which includes types incompatible with the return type mandated by Organizations\Entity\Org...nInterface::getImages() of Core\Entity\ImageSet. Consider adding a type-check to rule them out.
Loading history...
621
    }
622 3
623 2
    /**
624
     *
625
     *
626 3
     * @return self
627
     */
628
    public function removeImages()
629
    {
630
        $this->images = null;
631
632
        return $this;
633
    }
634 1
635
    /**
636 1
     * Sets the Contact Data of an organization
637
     *
638 1
     * @param EntityInterface $contact
639
     *
640
     * @return $this
641
     */
642
    public function setContact(EntityInterface $contact = null)
643
    {
644
        if (!$contact instanceof OrganizationContact) {
645
            $contact = new OrganizationContact($contact);
0 ignored issues
show
Unused Code introduced by
The call to Organizations\Entity\Org...nContact::__construct() has too many arguments starting with $contact. ( Ignorable by Annotation )

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

645
            $contact = /** @scrutinizer ignore-call */ new OrganizationContact($contact);

This check compares calls to functions or methods with their respective definitions. If the call has more 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...
646
        }
647
        $this->contact = $contact;
648 1
649
        return $this;
650 1
    }
651
652
    /**
653 1
     * Gets the contact Data of an organization
654
     *
655 1
     * @return OrganizationContact
656
     */
657
    public function getContact()
658
    {
659
        if (!$this->contact instanceof OrganizationContact) {
660
            $this->contact = new OrganizationContact();
661
        }
662
663 1
        return $this->contact;
664
    }
665 1
666
    /**
667
     * Gets the default description of an organization.
668
     *
669 1
     * This description is used as the default of the company_description
670
     * used in a job template
671
     *
672
     * @return string
673
     */
674
    public function getDescription()
675
    {
676
        return $this->description;
677
    }
678
679
    /**
680 1
     * Set the default description af an organization
681
     *
682 1
     * @param string $description
683
     *
684
     * @return $this
685
     */
686
    public function setDescription($description)
687
    {
688
        $this->description = $description;
689
690
        return $this;
691
    }
692 1
693
    /**
694 1
     * Sets the the list of employees
695
     *
696 1
     * @param Collection $employees
697
     *
698
     * @return $this
699
     */
700
    public function setEmployees(Collection $employees)
701
    {
702
        /* todo: Throw exception or at least log incidents, where employees are added to "hiring orgs" */
703
        if (!$this->isHiringOrganization()) {
704
            $this->employees = $employees;
705
        }
706 5
707
        return $this;
708
    }
709 5
710 5
    /**
711
     * Gets the list of employees
712
     *
713 5
     * @return ArrayCollection|Collection
714
     */
715
    public function getEmployees()
716
    {
717
        if ($this->isHiringOrganization()) {
718
            // Always return empty list, as we never have employees in this case.
719
            return new ArrayCollection();
720
        }
721 5
722
        if (!$this->employees) {
723 5
            $this->setEmployees(new ArrayCollection());
724
        }
725 1
726
        return $this->employees;
727
    }
728 5
729 2
    /**
730
     * Gets an employee by User or ID.
731
     *
732 5
     * @param UserInterface|string $userOrId
733
     *
734
     * @return mixed|null
735
     */
736
    public function getEmployee($userOrId)
737
    {
738
        $employees = $this->getEmployees();
739
        $userId    = $userOrId instanceof \Auth\Entity\UserInterface ? $userOrId->getId() : $userOrId;
740
741
        foreach ($employees as $employee) {
742 1
            if ($employee->getUser()->getId() == $userId) {
743
                return $employee;
744 1
            }
745 1
        }
746
747 1
        return null;
748 1
    }
749 1
750
    /**
751
     * Gets a list of Employees by a user role
752
     *
753 1
     * @param string $role
754
     *
755
     * @return ArrayCollection
756
     */
757
    public function getEmployeesByRole($role)
758
    {
759
        $employees = new ArrayCollection();
760
761
        /* @var \Organizations\Entity\Employee $employee */
762
        foreach ($this->getEmployees() as $employee) {
763 1
            if ($role === $employee->getRole()) {
764
                $employees->add($employee);
765 1
            }
766
        }
767
768 1
        return $employees;
769 1
    }
770 1
771
    public function setUser(UserInterface $user)
772
    {
773
        if ($this->user) {
774 1
            $this->getPermissions()->revoke($this->user, Permissions::PERMISSION_ALL, false);
775
        }
776
        $this->user = $user;
777 2
        $this->getPermissions()->grant($user, Permissions::PERMISSION_ALL);
778
779 2
        return $this;
780
    }
781
782 2
    /**
783 2
     * Gets the owner of the organization
784
     *
785 2
     * @return UserInterface
786
     */
787
    public function getUser()
788
    {
789
        return $this->user;
790
    }
791
792
    /**
793 3
     * Gets the Jobs of an organization
794
     *
795 3
     * @return Collection
796
     */
797
    public function getJobs()
798
    {
799
        return $this->jobs;
800
    }
801
802
    /**
803 1
     * Gets default values of an organizations job template
804
     *
805 1
     * @return TemplateInterface
806
     */
807
    public function getTemplate()
808
    {
809
        if (null === $this->template) {
810
            $this->template = new Template();
811
        }
812
813 2
        return $this->template;
814
    }
815 2
816 1
    /**
817
     * Sets default values of an organizations job template
818
     *
819 2
     * @return self
820
     */
821
    public function setTemplate(TemplateInterface $template)
822
    {
823
        $this->template = $template;
824
825
        return $this;
826
    }
827 1
828
    /**
829 1
     * Gets Workflow Settings
830
     *
831 1
     * @return WorkflowSettings|WorkflowSettingsInterface
832
     */
833
    public function getWorkflowSettings()
834
    {
835
        if (null == $this->workflowSettings) {
836
            $this->workflowSettings = new WorkflowSettings();
837
        }
838
839 1
        return $this->workflowSettings;
840
    }
841 1
842
    /**
843
     * Sets Workflow Settings
844
     *
845 1
     * @param $workflowSettings
846
     *
847
     * @return self
848
     */
849
    public function setWorkflowSettings($workflowSettings)
850
    {
851
        $this->workflowSettings = $workflowSettings;
852
853
        return $this;
854
    }
855
}
856