Completed
Push — develop ( 716766...f67c03 )
by Nate
12:05
created

OrganizationQuery   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 204
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 12
dl 0
loc 204
ccs 0
cts 98
cp 0
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A beforePrepare() 0 15 1
A prepareAttributes() 0 10 3
A prepareRelationsParams() 0 15 4
A joinOrganizationUserTable() 0 11 1
A joinOrganizationTypeTable() 0 11 1
A applyUserParam() 0 11 2
A applyUserGroupParam() 0 17 2
A applyUserTypeParam() 0 17 2
A applyTypeParam() 0 11 2
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\db;
10
11
use craft\db\Query;
12
use craft\elements\db\ElementQuery;
13
use craft\helpers\Db;
14
use craft\records\UserGroup_User as UserGroupUsersRecord;
15
use flipbox\ember\db\traits\UserAttribute;
16
use flipbox\ember\db\traits\UserGroupAttribute;
17
use flipbox\organizations\db\traits\OrganizationTypeAttribute;
18
use flipbox\organizations\db\traits\UserTypeAttribute;
19
use flipbox\organizations\elements\Organization as OrganizationElement;
20
use flipbox\organizations\records\Organization as OrganizationRecord;
21
use flipbox\organizations\records\OrganizationTypeAssociation as TypeAssociationRecord;
22
use flipbox\organizations\records\UserAssociation as OrganizationUsersRecord;
23
use flipbox\organizations\records\UserTypeAssociation as UserTypeAssociationRecord;
24
25
/**
26
 * @author Flipbox Factory <[email protected]>
27
 * @since 1.0.0
28
 *
29
 * @property Query $query
30
 * @property Query $subQuery
31
 *
32
 * @method OrganizationElement one($db = null)
33
 * @method OrganizationElement[] all($db = null)
34
 * @method OrganizationElement[] getCachedResult()
35
 */
36
class OrganizationQuery extends ElementQuery
37
{
38
    use UserAttribute,
39
        UserGroupAttribute,
40
        UserTypeAttribute,
41
        OrganizationTypeAttribute;
42
43
    /**
44
     * @var string|string[]|null The organization state(s) that the resulting organizations must have.
45
     */
46
    public $state;
47
48
    /**
49
     * @var mixed When the resulting organizations must have joined.
50
     */
51
    public $dateJoined;
52
53
    /**
54
     * @inheritdoc
55
     */
56
    protected function beforePrepare(): bool
57
    {
58
        $alias = OrganizationRecord::tableAlias();
59
        $this->joinElementTable($alias);
60
61
        $this->query->select([
62
            $alias . '.state',
63
            $alias . '.dateJoined'
64
        ]);
65
66
        $this->prepareRelationsParams();
67
        $this->prepareAttributes($alias);
68
69
        return parent::beforePrepare();
70
    }
71
72
    /**
73
     * Prepares simple attributes
74
     *
75
     * @var string $alias
76
     */
77
    protected function prepareAttributes(string $alias)
78
    {
79
        if ($this->dateJoined) {
80
            $this->subQuery->andWhere(Db::parseDateParam($alias . '.dateJoined', $this->dateJoined));
81
        }
82
83
        if ($this->state) {
84
            $this->subQuery->andWhere(Db::parseParam($alias . '.state', $this->state));
85
        }
86
    }
87
88
    /**
89
     * Prepares relation params
90
     */
91
    protected function prepareRelationsParams()
92
    {
93
        // Type
94
        $this->applyTypeParam();
95
96
        if (empty($this->user) && empty($this->userGroup) && empty($this->userType)) {
97
            return;
98
        }
99
100
        $alias = $this->joinOrganizationUserTable();
101
102
        $this->applyUserParam($alias);
103
        $this->applyUserGroupParam($alias);
104
        $this->applyUserTypeParam($alias);
105
    }
106
107
108
    /************************************************************
109
     * JOIN TABLES
110
     ************************************************************/
111
112
    /**
113
     * @return string
114
     */
115
    protected function joinOrganizationUserTable(): string
116
    {
117
        $alias = OrganizationUsersRecord::tableAlias();
118
119
        $this->subQuery->leftJoin(
120
            OrganizationUsersRecord::tableName() . ' ' . $alias,
121
            '[[' . $alias . '.organizationId]] = [[elements.id]]'
122
        );
123
124
        return $alias;
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    protected function joinOrganizationTypeTable(): string
131
    {
132
        $alias = TypeAssociationRecord::tableAlias();
133
134
        $this->subQuery->leftJoin(
135
            TypeAssociationRecord::tableName() . ' ' . $alias,
136
            '[[' . $alias . '.organizationId]] = [[elements.id]]'
137
        );
138
139
        return $alias;
140
    }
141
142
143
    /************************************************************
144
     * USER
145
     ************************************************************/
146
147
    /**
148
     * @param string $alias
149
     *
150
     * @return void
151
     */
152
    protected function applyUserParam(string $alias)
153
    {
154
        if (empty($this->user)) {
155
            return;
156
        }
157
158
        $this->subQuery->andWhere(
159
            Db::parseParam($alias . '.userId', $this->parseUserValue($this->user))
160
        );
161
        $this->subQuery->distinct(true);
162
    }
163
164
165
    /************************************************************
166
     * USER GROUP
167
     ************************************************************/
168
169
    /**
170
     * @param string $alias
171
     *
172
     * @return void
173
     */
174
    protected function applyUserGroupParam(string $alias)
175
    {
176
        if (empty($this->userGroup)) {
177
            return;
178
        }
179
180
        $groupAlias = 'ug_user';
181
182
        $this->subQuery->innerJoin(
183
            UserGroupUsersRecord::tableName() . ' ' . $groupAlias,
184
            '[[' . $groupAlias . '.userId]] = [[' . $alias . '.userId]]'
185
        );
186
187
        $this->subQuery->andWhere(
188
            Db::parseParam($groupAlias . '.groupId', $this->parseUserGroupValue($this->userGroup))
189
        );
190
    }
191
192
193
    /************************************************************
194
     * USER COLLECTION
195
     ************************************************************/
196
197
    /**
198
     * @param string $alias
199
     *
200
     * @return void
201
     */
202
    protected function applyUserTypeParam(string $alias)
203
    {
204
        if (empty($this->userType)) {
205
            return;
206
        }
207
208
        $typeAlias = 'ut_user';
209
210
        $this->subQuery->innerJoin(
211
            UserTypeAssociationRecord::tableName() . ' ' . $typeAlias,
212
            '[[' . $typeAlias . '.userId]] = [[' . $alias . '.userId]]'
213
        );
214
215
        $this->subQuery->andWhere(
216
            Db::parseParam($typeAlias . '.typeId', $this->parseUserTypeValue($this->userType))
217
        );
218
    }
219
220
221
    /************************************************************
222
     * TYPE
223
     ************************************************************/
224
225
    /**
226
     * @return void
227
     */
228
    protected function applyTypeParam()
229
    {
230
        if (empty($this->organizationType)) {
231
            return;
232
        }
233
234
        $alias = $this->joinOrganizationTypeTable();
235
        $this->subQuery->andWhere(
236
            Db::parseParam($alias . '.typeId', $this->parseOrganizationTypeValue($this->organizationType))
237
        );
238
    }
239
}
240