Completed
Push — develop ( e45dbf...2d4657 )
by Nate
04:34
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
     * @param $enabled
72
     * @return $this
73
     */
74
    public function enabled($enabled)
75
    {
76
        $this->enabled = $enabled;
77
        return $this;
78
    }
79
80
    /**
81
     * @param $id
82
     * @return $this
83
     */
84
    public function id($id)
85
    {
86
        $this->id = $id;
87
        return $this;
88
    }
89
90
    /**
91
     * @param $name
92
     * @return $this
93
     */
94
    public function name($name)
95
    {
96
        $this->name = $name;
97
        return $this;
98
    }
99
100
    /**
101
     * @param $handle
102
     * @return $this
103
     */
104
    public function handle($handle)
105
    {
106
        $this->handle = $handle;
107
        return $this;
108
    }
109
110
    /**
111
     * @param $clientId
112
     * @return $this
113
     */
114
    public function clientId($clientId)
115
    {
116
        $this->clientId = $clientId;
117
        return $this;
118
    }
119
120
    /**
121
     * @param $clientSecret
122
     * @return $this
123
     */
124
    public function clientSecret($clientSecret)
125
    {
126
        $this->clientSecret = $clientSecret;
127
        return $this;
128
    }
129
130
    /**
131
     * @param $environment
132
     * @return $this
133
     */
134
    public function environment($environment)
135
    {
136
        $this->environment = $environment;
137
        return $this;
138
    }
139
140
    /**
141
     * @throws QueryAbortedException
142
     */
143
    protected function applyConditions()
144
    {
145
        // Is the query already doomed?
146
        if ($this->id !== null && empty($this->id)) {
147
            throw new QueryAbortedException();
148
        }
149
150
        if ($this->enabled !== null) {
151
            $this->andWhere(Db::parseParam('enabled', $this->enabled));
152
        }
153
154
        $attributes = ['id', 'name', 'handle', 'clientId', 'clientSecret'];
155
156
        foreach ($attributes as $attribute) {
157
            if (($value = $this->{$attribute}) !== null) {
158
                $this->andWhere(Db::parseParam($attribute, $value));
159
            }
160
        }
161
162
        $this->applyEnvironmentParam();
163
    }
164
165
    /*******************************************
166
     * PARAMS
167
     *******************************************/
168
169
    /**
170
     * Apply environment params
171
     */
172
    protected function applyEnvironmentParam()
173
    {
174
        if (empty($this->environment)) {
175
            return;
176
        }
177
178
        $alias = ProviderEnvironment::tableAlias();
179
180
        $this->leftJoin(
0 ignored issues
show
Bug introduced by
It seems like leftJoin() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
181
            ProviderEnvironment::tableName() . ' ' . $alias,
182
            '[[' . $alias . '.providerId]] = [[' . Provider::tableAlias() . '.id]]'
183
        );
184
        $this->andWhere(
185
            Db::parseParam($alias . '.environment', $this->environment)
186
        );
187
188
        $this->distinct(true);
0 ignored issues
show
Bug introduced by
It seems like distinct() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
189
    }
190
}
191