UserTypeQuery   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 252
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 9
dl 0
loc 252
ccs 0
cts 113
cp 0
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A id() 0 5 1
A setId() 0 4 1
A handle() 0 5 1
A setHandle() 0 4 1
A name() 0 5 1
A setName() 0 4 1
A init() 0 13 3
A prepare() 0 8 1
A prepareAttributes() 0 10 3
A prepareRelationsParams() 0 11 3
A joinUserTypeAssociationsTable() 0 15 2
A joinOrganizationUserAssociationTable() 0 16 2
A applyUserParam() 0 15 4
A applyOrganizationParam() 0 15 4
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\queries;
10
11
use craft\db\QueryAbortedException;
12
use craft\helpers\Db;
13
use flipbox\craft\ember\queries\AuditAttributesTrait;
14
use flipbox\craft\ember\queries\CacheableActiveQuery;
15
use flipbox\craft\ember\queries\UserAttributeTrait;
16
use flipbox\organizations\records\UserAssociation as OrganizationUsersRecord;
17
use flipbox\organizations\records\UserType as UserTypeRecord;
18
use flipbox\organizations\records\UserTypeAssociation as UserTypeAssociationsRecord;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 *
24
 * @method UserTypeRecord one($db = null)
25
 * @method UserTypeRecord[] all($db = null)
26
 * @method UserTypeRecord[] getCachedResult($db = null)
27
 */
28
class UserTypeQuery extends CacheableActiveQuery
29
{
30
    use OrganizationAttributeTrait,
31
        UserAttributeTrait,
32
        AuditAttributesTrait;
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public $orderBy = ['name' => SORT_ASC];
38
39
    /**
40
     * @var int|int[]|null
41
     */
42
    public $id;
43
44
    /**
45
     * @var string|string[]|null
46
     */
47
    public $handle;
48
49
    /**
50
     * @var string|string[]|null
51
     */
52
    public $name;
53
54
    /**
55
     * @param $id
56
     * @return static
57
     */
58
    public function id($id)
59
    {
60
        $this->id = $id;
61
        return $this;
62
    }
63
64
    /**
65
     * @param $id
66
     * @return static
67
     */
68
    public function setId($id)
69
    {
70
        return $this->id($id);
71
    }
72
73
74
    /**
75
     * @param $handle
76
     * @return static
77
     */
78
    public function handle($handle)
79
    {
80
        $this->handle = $handle;
81
        return $this;
82
    }
83
84
    /**
85
     * @param $handle
86
     * @return static
87
     */
88
    public function setHandle($handle)
89
    {
90
        return $this->handle($handle);
91
    }
92
93
    /**
94
     * @param $name
95
     * @return static
96
     */
97
    public function name($name)
98
    {
99
        $this->name = $name;
100
        return $this;
101
    }
102
103
    /**
104
     * @param $name
105
     * @return static
106
     */
107
    public function setName($name)
108
    {
109
        return $this->name($name);
110
    }
111
112
    /**
113
     * Flag if the table is already joined (to prevent subsequent joins)
114
     *
115
     * @var bool
116
     */
117
    private $typeAssociationTableJoined = false;
118
119
    /**
120
     * Flag if the table is already joined (to prevent subsequent joins)
121
     *
122
     * @var bool
123
     */
124
    private $userAssociationTableJoined = false;
125
126
    /**
127
     * @inheritdoc
128
     */
129
    public function init()
130
    {
131
        if ($this->select === null) {
132
            $this->select = [UserTypeRecord::tableAlias() . '.*'];
133
        }
134
135
        // Set table name
136
        if ($this->from === null) {
137
            $this->from([UserTypeRecord::tableName() . ' ' . UserTypeRecord::tableAlias()]);
138
        }
139
140
        parent::init();
141
    }
142
143
    /**
144
     * @inheritdoc
145
     * @throws QueryAbortedException
146
     */
147
    public function prepare($builder)
148
    {
149
        $this->applyAuditAttributeConditions();
150
        $this->prepareRelationsParams();
151
        $this->prepareAttributes();
152
153
        return parent::prepare($builder);
154
    }
155
156
    /**
157
     * Prepares simple attributes
158
     */
159
    protected function prepareAttributes()
160
    {
161
        $attributes = ['id', 'handle', 'name'];
162
163
        foreach ($attributes as $attribute) {
164
            if (null !== ($value = $this->{$attribute})) {
165
                $this->andWhere(Db::parseParam(UserTypeRecord::tableAlias() . '.' . $attribute, $value));
166
            }
167
        }
168
    }
169
170
    /**
171
     * Prepares relation params
172
     * @throws QueryAbortedException
173
     */
174
    protected function prepareRelationsParams()
175
    {
176
        if (empty($this->user) && empty($this->organization)) {
177
            return;
178
        }
179
180
        $alias = $this->joinOrganizationUserAssociationTable();
181
182
        $this->applyUserParam($alias);
183
        $this->applyOrganizationParam($alias);
184
    }
185
186
187
    /************************************************************
188
     * JOIN TABLES
189
     ************************************************************/
190
191
    /**
192
     * @return string
193
     */
194
    protected function joinUserTypeAssociationsTable(): string
195
    {
196
        $alias = UserTypeAssociationsRecord::tableAlias();
197
198
        if ($this->typeAssociationTableJoined === false) {
199
            $this->leftJoin(
200
                UserTypeAssociationsRecord::tableName() . ' ' . $alias,
201
                '[[' . UserTypeRecord::tableAlias() . '.id]]=[[' . $alias . '.typeId]]'
202
            );
203
204
            $this->typeAssociationTableJoined = true;
205
        }
206
207
        return $alias;
208
    }
209
210
    /**
211
     * @return string
212
     */
213
    protected function joinOrganizationUserAssociationTable(): string
214
    {
215
        $userAlias = OrganizationUsersRecord::tableAlias();
216
217
        if ($this->userAssociationTableJoined === false) {
218
            $typeAlias = $this->joinUserTypeAssociationsTable();
219
            $this->leftJoin(
220
                OrganizationUsersRecord::tableName() . ' ' . $userAlias,
221
                '[[' . $userAlias . '.id]] = [[' . $typeAlias . '.userId]]'
222
            );
223
224
            $this->userAssociationTableJoined = true;
225
        }
226
227
        return $userAlias;
228
    }
229
230
231
    /************************************************************
232
     * USER
233
     ************************************************************/
234
235
    /**
236
     * @param string $alias
237
     * @throws QueryAbortedException
238
     */
239
    protected function applyUserParam(string $alias)
240
    {
241
        // Is the query already doomed?
242
        if ($this->user !== null && empty($this->user)) {
243
            throw new QueryAbortedException();
244
        }
245
246
        if (empty($this->user)) {
247
            return;
248
        }
249
250
        $this->andWhere(
251
            Db::parseParam($alias . '.userId', $this->parseUserValue($this->user))
252
        );
253
    }
254
255
256
    /************************************************************
257
     * ORGANIZATION
258
     ************************************************************/
259
260
    /**
261
     * @param string $alias
262
     * @throws QueryAbortedException
263
     */
264
    protected function applyOrganizationParam(string $alias)
265
    {
266
        // Is the query already doomed?
267
        if ($this->organization !== null && empty($this->organization)) {
268
            throw new QueryAbortedException();
269
        }
270
271
        if (empty($this->organization)) {
272
            return;
273
        }
274
275
        $this->andWhere(
276
            Db::parseParam($alias . '.organizationId', $this->parseOrganizationValue($this->organization))
277
        );
278
    }
279
}
280