Completed
Push — master ( 6f2475...9a35ef )
by Nate
16:01
created

UserQueryParamHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 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\objects;
10
11
use craft\elements\db\UserQuery;
12
use craft\helpers\Db;
13
use flipbox\organizations\db\behaviors\OrganizationAttributesToUserQueryBehavior;
14
use flipbox\organizations\db\traits\OrganizationAttribute;
15
use flipbox\organizations\db\traits\TypeAttribute;
16
use flipbox\organizations\db\traits\UserCategoryAttribute;
17
use flipbox\organizations\records\UserAssociation as OrganizationUsersRecord;
18
use flipbox\organizations\records\UserCategoryAssociation as UserCollectionUsersRecord;
19
use yii\base\BaseObject;
20
use yii\db\Query;
21
22
/**
23
 * @author Flipbox Factory <[email protected]>
24
 * @since 1.0.0
25
 */
26
class UserQueryParamHandler extends BaseObject
27
{
28
    use OrganizationAttribute,
29
        TypeAttribute,
30
        UserCategoryAttribute {
31
        setType as parentSetType;
32
        setUserCategory as parentSetUserCategory;
33
    }
34
35
    /**
36
     * @var OrganizationAttributesToUserQueryBehavior
37
     */
38
    private $owner;
39
40
    /**
41
     * @inheritdoc
42
     * @param OrganizationAttributesToUserQueryBehavior $owner
43
     */
44
    public function __construct(OrganizationAttributesToUserQueryBehavior $owner, array $config = [])
45
    {
46
        $this->owner = $owner;
47
        parent::__construct($config);
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function setUserCategory($value): UserQuery
54
    {
55
        $this->parentSetUserCategory($value);
56
        return $this->owner->owner;
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function setType($value): UserQuery
63
    {
64
        $this->parentSetType($value);
65
        return $this->owner->owner;
66
    }
67
68
    /**
69
     * @param UserQuery $query
70
     */
71
    public function applyParams(UserQuery $query)
72
    {
73
        if ($query->subQuery === null ||
74
            (
75
                $this->organization === null &&
76
                $this->type === null &&
77
                $this->userCategory === null
78
            )
79
        ) {
80
            return;
81
        }
82
83
        $alias = $this->joinOrganizationUserTable($query->subQuery);
84
85
        $this->applyOrganizationParam(
86
            $query->subQuery,
87
            $this->organization,
88
            $alias
89
        );
90
91
        $this->applyUserCategoryParam(
92
            $query->subQuery,
93
            $this->userCategory
94
        );
95
96
        // todo - implement types
97
    }
98
99
    /************************************************************
100
     * JOIN TABLES
101
     ************************************************************/
102
103
    /**
104
     * @inheritdoc
105
     */
106
    protected function joinOrganizationUserTable(Query $query): string
107
    {
108
        $alias = OrganizationUsersRecord::tableAlias();
109
110
        $query->leftJoin(
111
            OrganizationUsersRecord::tableName() . ' ' . $alias,
112
            '[[' . $alias . '.userId]] = [[elements.id]]'
113
        );
114
115
        return $alias;
116
    }
117
118
    /**
119
     * @inheritdoc
120
     */
121
    protected function joinOrganizationUserCollectionTable(Query $query): string
122
    {
123
        $alias = UserCollectionUsersRecord::tableAlias();
124
        $orgAlias = OrganizationUsersRecord::tableAlias();
125
        $query->leftJoin(
126
            UserCollectionUsersRecord::tableName() . ' ' . $alias,
127
            '[[' . $alias . '.userId]] = [[' . $orgAlias . '.id]]'
128
        );
129
130
        return $alias;
131
    }
132
133
134
    /************************************************************
135
     * ORGANIZATION
136
     ************************************************************/
137
138
    /**
139
     * @param Query $query
140
     * @param $organization
141
     * @param string $alias
142
     */
143
    protected function applyOrganizationParam(Query $query, $organization, string $alias)
144
    {
145
        if (empty($organization)) {
146
            return;
147
        }
148
149
        $query->andWhere(
150
            Db::parseParam($alias . '.organizationId', $this->parseOrganizationValue($organization))
151
        );
152
    }
153
154
155
    /************************************************************
156
     * USER CATEGORY
157
     ************************************************************/
158
159
    /**
160
     * @param Query $query
161
     * @param $category
162
     */
163
    protected function applyUserCategoryParam(Query $query, $category)
164
    {
165
        if (empty($category)) {
166
            return;
167
        }
168
169
        $alias = $this->joinOrganizationUserCollectionTable($query);
170
        $query->andWhere(
171
            Db::parseParam($alias . '.categoryId', $this->parseUserCategoryValue($category))
172
        );
173
174
        $query->distinct(true);
175
    }
176
}
177