Completed
Push — master ( 04ac75...81967b )
by Nate
17:06
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\ProviderInstance;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
trait ProviderAttributes
21
{
22
    use BaseProviderAttributes;
23
24
    /**
25
     * @var bool|null The enabled state
26
     */
27
    public $enabled = true;
28
29
    /**
30
     * @var int|int[]|false|null The model ID(s). Prefix IDs with "not " to exclude them.
31
     */
32
    public $id;
33
34
    /**
35
     * @var string|string[]|null The provider name(s). Prefix IDs with "not " to exclude them.
36
     */
37
    public $name;
38
39
    /**
40
     * @var string|string[]|null The provider handle(s). Prefix IDs with "not " to exclude them.
41
     */
42
    public $handle;
43
44
    /**
45
     * @param $enabled
46
     * @return $this
47
     */
48
    public function enabled($enabled)
49
    {
50
        $this->enabled = $enabled;
51
        return $this;
52
    }
53
54
    /**
55
     * @param $id
56
     * @return $this
57
     */
58
    public function id($id)
59
    {
60
        $this->id = $id;
61
        return $this;
62
    }
63
64
    /**
65
     * @param $name
66
     * @return $this
67
     */
68
    public function name($name)
69
    {
70
        $this->name = $name;
71
        return $this;
72
    }
73
74
    /**
75
     * @param $handle
76
     * @return $this
77
     */
78
    public function handle($handle)
79
    {
80
        $this->handle = $handle;
81
        return $this;
82
    }
83
84
    /**
85
     * @throws QueryAbortedException
86
     */
87
    protected function applyConditions()
88
    {
89
        // Is the query already doomed?
90
        if ($this->id !== null && empty($this->id)) {
91
            throw new QueryAbortedException();
92
        }
93
94
        if ($this->enabled !== null) {
95
            $this->andWhere(Db::parseParam('enabled', $this->enabled));
96
        }
97
98
        $this->applyProviderParam();
99
        $this->applySettingsParam();
100
        $this->applyEnvironmentParams();
101
    }
102
103
    /*******************************************
104
     * PARAMS
105
     *******************************************/
106
107
    /**
108
     * Apply environment params
109
     */
110
    protected function applyProviderParam()
111
    {
112
        $attributes = ['id', 'name', 'handle'];
113
114
        foreach ($attributes as $attribute) {
115
            if (($value = $this->{$attribute}) !== null) {
116
                $this->andWhere(Db::parseParam(Provider::tableAlias() . '.' . $attribute, $value));
117
            }
118
        }
119
    }
120
121
    /**
122
     * Apply environment params
123
     */
124
    protected function applySettingsParam()
125
    {
126
        $alias = ProviderInstance::tableAlias();
127
128
        $this->leftJoin(
129
            ProviderInstance::tableName() . ' ' . $alias,
130
            '[[' . $alias . '.providerId]] = [[' . Provider::tableAlias() . '.id]]'
131
        );
132
133
        $attributes = ['clientId', 'clientSecret'];
134
135
        foreach ($attributes as $attribute) {
136
            if (null !== ($value = $this->{$attribute})) {
137
                $this->andWhere(Db::parseParam($alias . '.' . $attribute, $value));
138
            }
139
        }
140
141
        $this->distinct(true);
142
    }
143
}
144