Completed
Push — master ( fa5f61...c43c81 )
by Nate
04:41
created

UserAssociationQuery::applyUserParam()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 12
cp 0
rs 9.7666
c 0
b 0
f 0
cc 4
nc 3
nop 0
crap 20
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\CacheableActiveQuery;
14
use flipbox\craft\ember\queries\UserAttributeTrait;
15
use flipbox\organizations\records\UserAssociation;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 *
21
 * @method UserAssociation one($db = null)
22
 * @method UserAssociation[] all($db = null)
23
 * @method UserAssociation[] getCachedResult($db = null)
24
 */
25
class UserAssociationQuery extends CacheableActiveQuery
26
{
27
    use UserAttributeTrait,
28
        OrganizationAttributeTrait;
29
30
    /**
31
     * @var string|string[]|null
32
     */
33
    public $state;
34
35
    /**
36
     * @param string|string[]|null $value
37
     * @return static The query object
38
     */
39
    public function setState($value)
40
    {
41
        $this->state = $value;
42
        return $this;
43
    }
44
45
    /**
46
     * @param string|string[]|null $value
47
     * @return static The query object
48
     */
49
    public function state($value)
50
    {
51
        return $this->setState($value);
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function init()
58
    {
59
        $this->from([
60
            UserAssociation::tableName() . ' ' . UserAssociation::tableAlias()
61
        ]);
62
63
        parent::init();
64
    }
65
66
    /**
67
     * @inheritdoc
68
     * @throws QueryAbortedException
69
     */
70
    public function prepare($builder)
71
    {
72
        $attributes = ['state'];
73
74
        foreach ($attributes as $attribute) {
75
            if (null !== ($value = $this->{$attribute})) {
76
                $this->andWhere(Db::parseParam($attribute, $value));
77
            }
78
        }
79
80
        $this->applyUserParam();
81
        $this->applyOrganizationParam();
82
83
        return parent::prepare($builder);
84
    }
85
86
    /**
87
     * @return void
88
     * @throws QueryAbortedException
89
     */
90
    protected function applyUserParam()
91
    {
92
        // Is the query already doomed?
93
        if ($this->user !== null && empty($this->user)) {
94
            throw new QueryAbortedException();
95
        }
96
97
        if (empty($this->user)) {
98
            return;
99
        }
100
101
        $this->andWhere(
102
            Db::parseParam('userId', $this->parseUserValue($this->user))
103
        );
104
    }
105
106
    /**
107
     * @return void
108
     * @throws QueryAbortedException
109
     */
110
    protected function applyOrganizationParam()
111
    {
112
        // Is the query already doomed?
113
        if ($this->organization !== null && empty($this->organization)) {
114
            throw new QueryAbortedException();
115
        }
116
117
        if (empty($this->organization)) {
118
            return;
119
        }
120
121
        $this->andWhere(
122
            Db::parseParam('organizationId', $this->parseOrganizationValue($this->organization))
123
        );
124
    }
125
}
126