Completed
Push — master ( 04ac75...81967b )
by Nate
17:06
created

BaseProviderAttributes::applyEnvironmentParams()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 14
cp 0
rs 9.7333
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\helpers\Db;
12
use flipbox\patron\records\ProviderEnvironment;
13
use flipbox\patron\records\ProviderInstance;
14
use yii\db\Expression;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
trait BaseProviderAttributes
21
{
22
    /**
23
     * @var string|string[]|null The client Id(s). Prefix IDs with "not " to exclude them.
24
     */
25
    public $clientId;
26
27
    /**
28
     * @var string|string[]|null The client secret(s). Prefix IDs with "not " to exclude them.
29
     */
30
    public $clientSecret;
31
32
    /**
33
     * @var string|string[]|null The environment(s). Prefix with "not " to exclude them.
34
     */
35
    public $environment;
36
37
    /**
38
     * Adds an additional WHERE condition to the existing one.
39
     * The new condition and the existing one will be joined using the `AND` operator.
40
     * @param string|array|Expression $condition the new WHERE condition. Please refer to [[where()]]
41
     * on how to specify this parameter.
42
     * @param array $params the parameters (name => value) to be bound to the query.
43
     * @return $this the query object itself
44
     * @see where()
45
     * @see orWhere()
46
     */
47
    abstract public function andWhere($condition, $params = []);
48
49
    /**
50
     * Appends a LEFT OUTER JOIN part to the query.
51
     * @param string|array $table the table to be joined.
52
     *
53
     * Use a string to represent the name of the table to be joined.
54
     * The table name can contain a schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u').
55
     * The method will automatically quote the table name unless it contains some parenthesis
56
     * (which means the table is given as a sub-query or DB expression).
57
     *
58
     * Use an array to represent joining with a sub-query. The array must contain only one element.
59
     * The value must be a [[Query]] object representing the sub-query while the corresponding key
60
     * represents the alias for the sub-query.
61
     *
62
     * @param string|array $on the join condition that should appear in the ON part.
63
     * Please refer to [[join()]] on how to specify this parameter.
64
     * @param array $params the parameters (name => value) to be bound to the query
65
     * @return $this the query object itself
66
     */
67
    abstract public function leftJoin($table, $on = '', $params = []);
68
69
    /**
70
     * Sets the value indicating whether to SELECT DISTINCT or not.
71
     * @param bool $value whether to SELECT DISTINCT or not.
72
     * @return $this the query object itself
73
     */
74
    abstract public function distinct($value = true);
75
76
    /**
77
     * @param $clientId
78
     * @return $this
79
     */
80
    public function clientId($clientId)
81
    {
82
        $this->clientId = $clientId;
83
        return $this;
84
    }
85
86
    /**
87
     * @param $clientSecret
88
     * @return $this
89
     */
90
    public function clientSecret($clientSecret)
91
    {
92
        $this->clientSecret = $clientSecret;
93
        return $this;
94
    }
95
96
    /**
97
     * @param $environment
98
     * @return $this
99
     */
100
    public function environment($environment)
101
    {
102
        $this->environment = $environment;
103
        return $this;
104
    }
105
106
    /**
107
     * Apply environment params
108
     */
109
    protected function applyEnvironmentParams()
110
    {
111
        if (empty($this->environment)) {
112
            return;
113
        }
114
115
        $alias = ProviderEnvironment::tableAlias();
116
117
        $this->leftJoin(
118
            ProviderEnvironment::tableName() . ' ' . $alias,
119
            '[[' . $alias . '.instanceId]] = [[' . ProviderInstance::tableAlias() . '.id]]'
120
        );
121
        $this->andWhere(
122
            Db::parseParam($alias . '.environment', $this->environment)
123
        );
124
    }
125
}
126