Completed
Push — master ( 829e7b...9495f2 )
by Nate
01:24
created

internalResolveOrganization()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 14
cp 0
rs 8.8657
c 0
b 0
f 0
cc 6
nc 5
nop 1
crap 42
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember
7
 */
8
9
namespace flipbox\organizations\objects;
10
11
use Craft;
12
use flipbox\craft\ember\helpers\ObjectHelper;
13
use flipbox\organizations\elements\Organization;
14
15
/**
16
 * @property int|null $organizationId
17
 * @property Organization|null $organization
18
 *
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
trait OrganizationMutatorTrait
23
{
24
    /**
25
     * @var Organization|null
26
     */
27
    private $organization;
28
29
    /**
30
     * Set associated organizationId
31
     *
32
     * @param $id
33
     * @return $this
34
     */
35
    public function setOrganizationId(int $id)
36
    {
37
        $this->organizationId = $id;
38
        return $this;
39
    }
40
41
    /**
42
     * Get associated organizationId
43
     *
44
     * @return int|null
45
     */
46
    public function getOrganizationId()
47
    {
48
        if (null === $this->organizationId && null !== $this->organization) {
49
            $this->organizationId = $this->organization->id;
50
        }
51
52
        return $this->organizationId;
53
    }
54
55
    /**
56
     * AssociateUserToOrganization a organization
57
     *
58
     * @param mixed $organization
59
     * @return $this
60
     */
61
    public function setOrganization($organization = null)
62
    {
63
        $this->organization = null;
64
65
        if (null === ($organization = $this->internalResolveOrganization($organization))) {
66
            $this->organization = $this->organizationId = null;
67
        } else {
68
            $this->organizationId = $organization->id;
69
            $this->organization = $organization;
0 ignored issues
show
Documentation Bug introduced by
It seems like $organization of type object<craft\base\Element> or array<integer,object<craft\base\Element>> is incompatible with the declared type object<flipbox\organizat...ents\Organization>|null of property $organization.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
70
        }
71
72
        return $this;
73
    }
74
75
    /**
76
     * @return Organization|null
77
     */
78
    public function getOrganization()
79
    {
80
        if ($this->organization === null) {
81
            $organization = $this->resolveOrganization();
82
            $this->setOrganization($organization);
83
            return $organization;
84
        }
85
86
        $organizationId = $this->organizationId;
87
        if ($organizationId !== null &&
88
            $organizationId !== $this->organization->id
89
        ) {
90
            $this->organization = null;
91
            return $this->getOrganization();
92
        }
93
94
        return $this->organization;
95
    }
96
97
    /**
98
     * @return Organization|null
99
     */
100
    protected function resolveOrganization()
101
    {
102
        if ($model = $this->resolveOrganizationFromId()) {
103
            return $model;
104
        }
105
106
        return null;
107
    }
108
109
    /**
110
     * @return Organization|null
111
     */
112
    private function resolveOrganizationFromId()
113
    {
114
        if (null === $this->organizationId) {
115
            return null;
116
        }
117
118
        return Organization::findOne($this->organizationId);
119
    }
120
121
    /**
122
     * @param $organization
123
     * @return Organization|null
124
     */
125
    protected function internalResolveOrganization($organization = null)
126
    {
127
        if (null === $organization) {
128
            return null;
129
        }
130
131
        if ($organization instanceof Organization) {
132
            return $organization;
133
        }
134
135
        if (is_numeric($organization) || is_string($organization)) {
136
            return Organization::findOne($organization);
137
        }
138
139
        try {
140
            $object = Craft::createObject(Organization::class, [$organization]);
141
        } catch (\Exception $e) {
142
            $object = new Organization();
143
            ObjectHelper::populate(
144
                $object,
145
                $organization
146
            );
147
        }
148
149
        /** @var Organization $object */
150
        return $object;
151
    }
152
}
153