Completed
Push — develop ( bcbf28...a87a8b )
by Nate
03:33
created

resolveObjectInternal()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organizations\relationships;
10
11
use craft\helpers\ArrayHelper;
12
use craft\helpers\Json;
13
use flipbox\craft\ember\helpers\QueryHelper;
14
use flipbox\organizations\elements\Organization;
15
use flipbox\organizations\Organizations;
16
use flipbox\organizations\queries\OrganizationTypeAssociationQuery;
17
use flipbox\organizations\records\OrganizationType;
18
use flipbox\organizations\records\OrganizationTypeAssociation;
19
use flipbox\organizations\records\UserAssociation;
20
use Tightenco\Collect\Support\Collection;
21
22
/**
23
 * Manages Organization Types associated to Organizations
24
 *
25
 * @author Flipbox Factory <[email protected]>
26
 * @since 2.0.0
27
 *
28
 * @method OrganizationTypeAssociation findOrCreate($object)
29
 * @method OrganizationTypeAssociation findOne($object = null)
30
 * @method OrganizationTypeAssociation findOrFail($object)
31
 */
32
class OrganizationTypeRelationship implements RelationshipInterface
33
{
34
    use RelationshipTrait;
35
36
    /**
37
     * @var Organization
38
     */
39
    private $organization;
40
41
    /**
42
     * @param Organization $organization
43
     */
44
    public function __construct(Organization $organization)
45
    {
46
        $this->organization = $organization;
47
    }
48
49
50
    /************************************************************
51
     * COLLECTION
52
     ************************************************************/
53
54
    /**
55
     * Get an array of types associated to an organization
56
     *
57
     * @return OrganizationType[]|Collection
58
     */
59
    public function getCollection(): Collection
60
    {
61
        return $this->getRelationships()
62
            ->sortBy('sortOrder')
63
            ->pluck('type');
64
    }
65
66
    /**
67
     * @return Collection
68
     */
69
    protected function existingRelationships(): Collection
70
    {
71
        return new Collection(
72
            $this->query()
73
                ->with('typeRecord')
74
                ->all()
75
        );
76
    }
77
78
79
    /************************************************************
80
     * QUERY
81
     ************************************************************/
82
83
    /**
84
     * @return OrganizationTypeAssociationQuery
85
     */
86
    protected function query(): OrganizationTypeAssociationQuery
87
    {
88
        return OrganizationTypeAssociation::find()
89
            ->setOrganizationId($this->organization->getId() ?: false)
0 ignored issues
show
Security Bug introduced by
It seems like $this->organization->getId() ?: false can also be of type false; however, flipbox\organizations\qu...it::setOrganizationId() does only seem to accept string|array<integer,str...nts\Organization>>|null, did you maybe forget to handle an error condition?
Loading history...
90
            ->orderBy([
91
                'sortOrder' => SORT_ASC
92
            ])
93
            ->limit(null);
94
    }
95
96
    /**
97
     * @param OrganizationTypeAssociation|OrganizationType|int|string $type
98
     * @return OrganizationTypeAssociation
99
     */
100
    protected function create($type): OrganizationTypeAssociation
101
    {
102
        if ($type instanceof OrganizationTypeAssociation) {
103
            return $type;
104
        }
105
106
        return (new OrganizationTypeAssociation())
107
            ->setOrganization($this->organization)
108
            ->setType($this->resolveObject($type));
109
    }
110
111
112
    /*******************************************
113
     * SAVE
114
     *******************************************/
115
116
    /**
117
     * @inheritDoc
118
     */
119
    protected function delta(): array
120
    {
121
        $existingAssociations = $this->query()
122
            ->indexBy('typeId')
123
            ->all();
124
125
        $associations = [];
126
        $order = 1;
127
        /** @var OrganizationTypeAssociation $newAssociation */
128
        foreach ($this->getRelationships()->sortBy('sortOrder') as $newAssociation) {
129
            if (null === ($association = ArrayHelper::remove(
130
                $existingAssociations,
131
                $newAssociation->getTypeId()
132
            ))) {
133
                $association = $newAssociation;
134
            }
135
136
            $association->sortOrder = $order++;
137
138
            $association->ignoreSortOrder();
139
140
            $associations[] = $association;
141
        }
142
143
        return [$associations, $existingAssociations];
144
    }
145
146
147
    /*******************************************
148
     * COLLECTION UTILS
149
     *******************************************/
150
151
    /**
152
     * @inheritDoc
153
     */
154
    protected function insertCollection(Collection $collection, OrganizationTypeAssociation $association)
155
    {
156
        if ($association->sortOrder > 0) {
157
            $collection->splice($association->sortOrder - 1, 0, [$association]);
158
            return;
159
        }
160
161
        $collection->push($association);
162
    }
163
164
    /**
165
     * @inheritDoc
166
     */
167
    protected function updateCollection(Collection $collection, OrganizationTypeAssociation $association)
168
    {
169
        if ($key = $this->findKey($association)) {
170
            $collection->offsetUnset($key);
171
        }
172
173
        $this->insertCollection($collection, $association);
174
    }
175
176
177
    /*******************************************
178
     * UTILS
179
     *******************************************/
180
181
    /**
182
     * @param OrganizationTypeAssociation|OrganizationType|int|array|null $object
183
     * @return int|null
184
     */
185
    protected function findKey($object = null)
186
    {
187
        if ($object instanceof OrganizationTypeAssociation) {
188
            return $this->findRelationshipKey($object->getTypeId());
189
        }
190
191
        if (null === ($type = $this->resolveObject($object))) {
192
            return null;
193
        }
194
195
        return $this->findRelationshipKey($type->id);
196
    }
197
198
    /**
199
     * @param $identifier
200
     * @return int|string|null
201
     */
202
    protected function findRelationshipKey($identifier)
203
    {
204
        /** @var OrganizationTypeAssociation $association */
205
        foreach ($this->getRelationships()->all() as $key => $association) {
206
            if ($association->getTypeId() == $identifier) {
207
                return $key;
208
            }
209
        }
210
211
        return null;
212
    }
213
214
    /**
215
     * @param OrganizationTypeAssociation|OrganizationType|int|array $type
216
     * @return OrganizationType|null
217
     */
218
    protected function resolveObjectInternal($type)
219
    {
220
        if ($type instanceof OrganizationTypeAssociation) {
221
            return $type->getType();
222
        }
223
224
        if ($type instanceof OrganizationType) {
225
            return $type;
226
        }
227
228
        return OrganizationType::findOne($type);
229
    }
230
}
231