Completed
Push — master ( e45dbf...3a73d5 )
by Nate
05:43 queued 04:08
created

ProviderAttributes::applyEnvironmentParam()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 15
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/patron/license
6
 * @link       https://www.flipboxfactory.com/patron/domains/
7
 */
8
9
namespace flipbox\patron\db\traits;
10
11
use craft\db\QueryAbortedException;
12
use craft\helpers\Db;
13
use flipbox\patron\records\Provider;
14
use flipbox\patron\records\ProviderEnvironment;
15
use yii\db\Expression;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
trait ProviderAttributes
22
{
23
    /**
24
     * @var bool|null The enabled state
25
     */
26
    public $enabled = true;
27
28
    /**
29
     * @var int|int[]|false|null The model ID(s). Prefix IDs with "not " to exclude them.
30
     */
31
    public $id;
32
33
    /**
34
     * @var string|string[]|null The provider name(s). Prefix IDs with "not " to exclude them.
35
     */
36
    public $name;
37
38
    /**
39
     * @var string|string[]|null The provider handle(s). Prefix IDs with "not " to exclude them.
40
     */
41
    public $handle;
42
43
    /**
44
     * @var string|string[]|null The client Id(s). Prefix IDs with "not " to exclude them.
45
     */
46
    public $clientId;
47
48
    /**
49
     * @var string|string[]|null The client secret(s). Prefix IDs with "not " to exclude them.
50
     */
51
    public $clientSecret;
52
53
    /**
54
     * @var string|string[]|null The environment(s). Prefix with "not " to exclude them.
55
     */
56
    public $environment;
57
58
    /**
59
     * Adds an additional WHERE condition to the existing one.
60
     * The new condition and the existing one will be joined using the `AND` operator.
61
     * @param string|array|Expression $condition the new WHERE condition. Please refer to [[where()]]
62
     * on how to specify this parameter.
63
     * @param array $params the parameters (name => value) to be bound to the query.
64
     * @return $this the query object itself
65
     * @see where()
66
     * @see orWhere()
67
     */
68
    abstract public function andWhere($condition, $params = []);
69
70
    /**
71
     * Appends a LEFT OUTER JOIN part to the query.
72
     * @param string|array $table the table to be joined.
73
     *
74
     * Use a string to represent the name of the table to be joined.
75
     * The table name can contain a schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u').
76
     * The method will automatically quote the table name unless it contains some parenthesis
77
     * (which means the table is given as a sub-query or DB expression).
78
     *
79
     * Use an array to represent joining with a sub-query. The array must contain only one element.
80
     * The value must be a [[Query]] object representing the sub-query while the corresponding key
81
     * represents the alias for the sub-query.
82
     *
83
     * @param string|array $on the join condition that should appear in the ON part.
84
     * Please refer to [[join()]] on how to specify this parameter.
85
     * @param array $params the parameters (name => value) to be bound to the query
86
     * @return $this the query object itself
87
     */
88
    abstract public function leftJoin($table, $on = '', $params = []);
89
90
    /**
91
     * Sets the value indicating whether to SELECT DISTINCT or not.
92
     * @param bool $value whether to SELECT DISTINCT or not.
93
     * @return $this the query object itself
94
     */
95
    abstract public function distinct($value = true);
96
97
    /**
98
     * @param $enabled
99
     * @return $this
100
     */
101
    public function enabled($enabled)
102
    {
103
        $this->enabled = $enabled;
104
        return $this;
105
    }
106
107
    /**
108
     * @param $id
109
     * @return $this
110
     */
111
    public function id($id)
112
    {
113
        $this->id = $id;
114
        return $this;
115
    }
116
117
    /**
118
     * @param $name
119
     * @return $this
120
     */
121
    public function name($name)
122
    {
123
        $this->name = $name;
124
        return $this;
125
    }
126
127
    /**
128
     * @param $handle
129
     * @return $this
130
     */
131
    public function handle($handle)
132
    {
133
        $this->handle = $handle;
134
        return $this;
135
    }
136
137
    /**
138
     * @param $clientId
139
     * @return $this
140
     */
141
    public function clientId($clientId)
142
    {
143
        $this->clientId = $clientId;
144
        return $this;
145
    }
146
147
    /**
148
     * @param $clientSecret
149
     * @return $this
150
     */
151
    public function clientSecret($clientSecret)
152
    {
153
        $this->clientSecret = $clientSecret;
154
        return $this;
155
    }
156
157
    /**
158
     * @param $environment
159
     * @return $this
160
     */
161
    public function environment($environment)
162
    {
163
        $this->environment = $environment;
164
        return $this;
165
    }
166
167
    /**
168
     * @throws QueryAbortedException
169
     */
170
    protected function applyConditions()
171
    {
172
        // Is the query already doomed?
173
        if ($this->id !== null && empty($this->id)) {
174
            throw new QueryAbortedException();
175
        }
176
177
        if ($this->enabled !== null) {
178
            $this->andWhere(Db::parseParam('enabled', $this->enabled));
179
        }
180
181
        $attributes = ['id', 'name', 'handle', 'clientId', 'clientSecret'];
182
183
        foreach ($attributes as $attribute) {
184
            if (($value = $this->{$attribute}) !== null) {
185
                $this->andWhere(Db::parseParam($attribute, $value));
186
            }
187
        }
188
189
        $this->applyEnvironmentParam();
190
    }
191
192
    /*******************************************
193
     * PARAMS
194
     *******************************************/
195
196
    /**
197
     * Apply environment params
198
     */
199
    protected function applyEnvironmentParam()
200
    {
201
        if (empty($this->environment)) {
202
            return;
203
        }
204
205
        $alias = ProviderEnvironment::tableAlias();
206
207
        $this->leftJoin(
208
            ProviderEnvironment::tableName() . ' ' . $alias,
209
            '[[' . $alias . '.providerId]] = [[' . Provider::tableAlias() . '.id]]'
210
        );
211
        $this->andWhere(
212
            Db::parseParam($alias . '.environment', $this->environment)
213
        );
214
215
        $this->distinct(true);
216
    }
217
}
218