Completed
Push — master ( b27204...a2c64d )
by Nate
05:15 queued 02:48
created

UserQueryParamHandler   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 10
dl 0
loc 148
ccs 0
cts 80
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setUserType() 0 5 1
A applyParams() 0 24 5
A joinOrganizationUserTable() 0 21 3
A joinOrganizationUserTypeTable() 0 8 1
A applyOrganizationParam() 0 13 2
A applyUserTypeParam() 0 14 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\queries;
10
11
use craft\elements\db\UserQuery;
12
use craft\helpers\Db;
13
use flipbox\organizations\records\UserAssociation as OrganizationUsersRecord;
14
use flipbox\organizations\records\UserTypeAssociation as UserCollectionUsersRecord;
15
use yii\base\BaseObject;
16
use yii\db\Query;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
class UserQueryParamHandler extends BaseObject
23
{
24
    use OrganizationAttributeTrait,
25
        OrganizationTypeAttributeTrait,
26
        UserTypeAttributeTrait {
27
        setUserType as parentSetUserType;
28
    }
29
30
    /**
31
     * @var OrganizationAttributesToUserQueryBehavior
32
     */
33
    private $owner;
34
35
    /**
36
     * @inheritdoc
37
     * @param OrganizationAttributesToUserQueryBehavior $owner
38
     */
39
    public function __construct(OrganizationAttributesToUserQueryBehavior $owner, array $config = [])
40
    {
41
        $this->owner = $owner;
42
        parent::__construct($config);
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function setUserType($value): UserQuery
49
    {
50
        $this->parentSetUserType($value);
51
        return $this->owner->owner;
52
    }
53
54
    /**
55
     * @param UserQuery $query
56
     */
57
    public function applyParams(UserQuery $query)
58
    {
59
        if ($query->subQuery === null ||
60
            (
61
                $this->organization === null &&
62
                $this->organizationType === null &&
63
                $this->userType === null
64
            )
65
        ) {
66
            return;
67
        }
68
69
        $this->joinOrganizationUserTable($query);
70
71
        $this->applyOrganizationParam(
72
            $query,
73
            $this->organization
74
        );
75
76
        $this->applyUserTypeParam(
77
            $query,
78
            $this->userType
79
        );
80
    }
81
82
    /************************************************************
83
     * JOIN TABLES
84
     ************************************************************/
85
86
    /**
87
     * @inheritdoc
88
     */
89
    protected function joinOrganizationUserTable(UserQuery $query)
90
    {
91
        $alias = OrganizationUsersRecord::tableAlias();
92
93
        $query->subQuery->leftJoin(
94
            OrganizationUsersRecord::tableName() . ' ' . $alias,
95
            '[[' . $alias . '.userId]] = [[elements.id]]'
96
        );
97
98
        // Check if we're ordering by one of the association table's order columns
99
        if (is_array($query->orderBy)) {
100
            $columns = ['userOrder' => 'userOrder', 'organizationOrder' => 'organizationOrder'];
101
            $matches = array_intersect_key($columns, $query->orderBy);
102
103
            foreach ($matches as $param => $select) {
104
                $query->subQuery->addSelect([$alias . '.' . $select]);
105
            }
106
        }
107
108
        return $alias;
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114
    protected function joinOrganizationUserTypeTable(Query $query)
115
    {
116
        $alias = UserCollectionUsersRecord::tableAlias();
117
        $query->leftJoin(
118
            UserCollectionUsersRecord::tableName() . ' ' . $alias,
119
            '[[' . $alias . '.userId]] = [[' . OrganizationUsersRecord::tableAlias() . '.id]]'
120
        );
121
    }
122
123
124
    /************************************************************
125
     * ORGANIZATION
126
     ************************************************************/
127
128
    /**
129
     * @param UserQuery $query
130
     * @param $organization
131
     */
132
    protected function applyOrganizationParam(UserQuery $query, $organization)
133
    {
134
        if (empty($organization)) {
135
            return;
136
        }
137
138
        $query->subQuery->andWhere(
139
            Db::parseParam(
140
                OrganizationUsersRecord::tableAlias() . '.organizationId',
141
                $this->parseOrganizationValue($organization)
142
            )
143
        );
144
    }
145
146
147
    /************************************************************
148
     * USER CATEGORY
149
     ************************************************************/
150
151
    /**
152
     * @param UserQuery $query
153
     * @param $type
154
     */
155
    protected function applyUserTypeParam(UserQuery $query, $type)
156
    {
157
        if (empty($type)) {
158
            return;
159
        }
160
161
        $this->joinOrganizationUserTypeTable($query->subQuery);
0 ignored issues
show
Bug introduced by
It seems like $query->subQuery can be null; however, joinOrganizationUserTypeTable() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
162
        $query->subQuery->andWhere(
163
            Db::parseParam(
164
                UserCollectionUsersRecord::tableAlias() . '.typeId',
165
                $this->parseUserTypeValue($type)
166
            )
167
        );
168
    }
169
}
170