Completed
Push — develop ( e9f396...2b8a13 )
by Nate
04:23
created

UserAssociationManagerTrait::addMany()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 16
cp 0
rs 9.2728
c 0
b 0
f 0
cc 5
nc 16
nop 1
crap 30
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\objects;
10
11
use craft\base\ElementInterface;
12
use craft\helpers\ArrayHelper;
13
use DateTime;
14
use flipbox\organizations\queries\UserAssociationQuery;
15
use flipbox\organizations\records\UserAssociation;
16
use yii\db\QueryInterface;
17
18
/**
19
 * @property DateTime|null $dateJoined
20
 *
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.1.0
23
 */
24
trait UserAssociationManagerTrait
25
{
26
    use MutateableTrait;
27
28
    /**
29
     * @var UserAssociation[]|null
30
     */
31
    protected $associations;
32
33
    /**
34
     * @param null $object
35
     * @return int|null
36
     */
37
    abstract protected function findKey($object = null);
38
39
    /**
40
     * @param array $criteria
41
     * @return UserAssociationQuery
42
     */
43
    abstract public function query(array $criteria = []): UserAssociationQuery;
44
45
    /**
46
     * @param $object
47
     * @return UserAssociation
48
     */
49
    abstract public function create($object): UserAssociation;
50
51
    /**
52
     * @param UserAssociation|ElementInterface|int|string $object
53
     * @return UserAssociation
54
     */
55
    public function findOrCreate($object): UserAssociation
56
    {
57
        if (null === ($association = $this->findOne($object))) {
58
            $association = $this->create($object);
59
        }
60
61
        return $association;
62
    }
63
64
    /**
65
     * @return UserAssociation[]
66
     */
67
    public function findAll(): array
68
    {
69
        if (null === $this->associations) {
70
            $this->associations = $this->query()->all();
71
        }
72
73
        return $this->associations;
74
    }
75
76
    /**
77
     * @param UserAssociation|ElementInterface|int|string|null $object
78
     * @return UserAssociation
79
     */
80
    public function findOne($object = null): UserAssociation
81
    {
82
        if (null === ($key = $this->findKey($object))) {
83
            return null;
84
        }
85
86
        return $this->associations[$key];
87
    }
88
89
    /**
90
     * @param UserAssociation|ElementInterface|int|string $object
91
     * @return bool
92
     */
93
    public function exists($object): bool
94
    {
95
        return null !== $this->findKey($object);
96
    }
97
98
99
    /************************************************************
100
     * SET
101
     ************************************************************/
102
103
    /**
104
     * @param $objects
105
     * @return $this
106
     */
107
    public function setMany($objects)
108
    {
109
        if ($objects instanceof UserAssociationQuery) {
110
            $this->associations = $objects->all();
111
            return $this;
112
        }
113
114
        // Reset results
115
        $this->associations = [];
116
117
        if (!empty($objects)) {
118
            if (!is_array($objects)) {
119
                $objects = [$objects];
120
            }
121
122
            $this->addMany($objects);
123
        }
124
125
        return $this;
126
    }
127
128
129
    /************************************************************
130
     * ADD
131
     ************************************************************/
132
133
    /**
134
     * @param QueryInterface|ElementInterface[] $objects
135
     * @return $this
136
     */
137
    public function addMany($objects)
138
    {
139
        if ($objects instanceof QueryInterface) {
140
            $objects = $objects->all();
141
        }
142
143
        if (!is_array($objects)) {
144
            $objects = [$objects];
145
        }
146
147
        // In case a config is directly passed
148
        if (ArrayHelper::isAssociative($objects)) {
149
            $objects = [$objects];
150
        }
151
152
        foreach ($objects as $object) {
153
            $this->addOne($object);
154
        }
155
156
        return $this;
157
    }
158
159
    /**
160
     * Associate a user to an organization
161
     *
162
     * @param UserAssociation|ElementInterface|int|array $object
163
     * @return $this
164
     */
165
    public function addOne($object)
166
    {
167
        if (null === ($association = $this->findOne($object))) {
168
            $this->associations[] = $this->create($object);
169
170
            $this->mutated = true;
171
        }
172
173
        return $this;
174
    }
175
176
177
    /************************************************************
178
     * REMOVE
179
     ************************************************************/
180
181
    /**
182
     * Dissociate an array of user associations from an organization
183
     *
184
     * @param QueryInterface|ElementInterface[] $objects
185
     * @return $this
186
     */
187
    public function removeMany($objects)
188
    {
189
        if ($objects instanceof QueryInterface) {
190
            $objects = $objects->all();
191
        }
192
193
        if (!is_array($objects)) {
194
            $objects = [$objects];
195
        }
196
197
        // In case a config is directly passed
198
        if (ArrayHelper::isAssociative($objects)) {
199
            $objects = [$objects];
200
        }
201
202
        foreach ($objects as $object) {
203
            $this->removeOne($object);
204
        }
205
206
        return $this;
207
    }
208
209
    /**
210
     * Dissociate a user from an organization
211
     *
212
     * @param UserAssociation|ElementInterface|int|array
213
     * @return $this
214
     */
215
    public function removeOne($object)
216
    {
217
        if (null !== ($key = $this->findKey($object))) {
218
            unset($this->associations[$key]);
219
            $this->mutated = true;
220
        }
221
222
        return $this;
223
    }
224
225
    /**
226
     * Reset associations
227
     */
228
    public function reset()
229
    {
230
        $this->associations = null;
231
        $this->mutated = false;
232
        return $this;
233
    }
234
}
235