Completed
Push — master ( 19b2ae...0e0de4 )
by Nate
04:43
created

ProviderAttributesTrait::applyProviderConditions()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 18
cp 0
rs 8.9297
c 0
b 0
f 0
cc 6
nc 7
nop 0
crap 42
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\queries;
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 ProviderAttributesTrait
21
{
22
    use ProviderClientAttributesTrait,
23
        ProviderEnvironmentAttributesTrait;
24
25
    /**
26
     * @var bool|null The enabled state
27
     */
28
    public $enabled = true;
29
30
    /**
31
     * @var int|int[]|false|null The model ID(s). Prefix IDs with "not " to exclude them.
32
     */
33
    public $id;
34
35
    /**
36
     * @var string|string[]|null The provider name(s). Prefix IDs with "not " to exclude them.
37
     */
38
    public $name;
39
40
    /**
41
     * @var string|string[]|null The provider handle(s). Prefix IDs with "not " to exclude them.
42
     */
43
    public $handle;
44
45
    /**
46
     * @var string|string[]|null The provider classes(s). Prefix IDs with "not " to exclude them.
47
     */
48
    public $class;
49
50
    /**
51
     * @param $enabled
52
     * @return $this
53
     */
54
    public function enabled($enabled)
55
    {
56
        $this->enabled = $enabled;
57
        return $this;
58
    }
59
60
    /**
61
     * @param $id
62
     * @return $this
63
     */
64
    public function id($id)
65
    {
66
        $this->id = $id;
67
        return $this;
68
    }
69
70
    /**
71
     * @param $name
72
     * @return $this
73
     */
74
    public function name($name)
75
    {
76
        $this->name = $name;
77
        return $this;
78
    }
79
80
    /**
81
     * @param $handle
82
     * @return $this
83
     */
84
    public function handle($handle)
85
    {
86
        $this->handle = $handle;
87
        return $this;
88
    }
89
90
    /**
91
     * @param $class
92
     * @return $this
93
     */
94
    public function class($class)
95
    {
96
        $this->class = $class;
97
        return $this;
98
    }
99
100
    /**
101
     * @throws QueryAbortedException
102
     */
103
    protected function applyProviderConditions()
104
    {
105
        // Is the query already doomed?
106
        if ($this->id !== null && empty($this->id)) {
107
            throw new QueryAbortedException();
108
        }
109
110
        if ($this->enabled !== null) {
111
            $this->andWhere(Db::parseParam('enabled', $this->enabled));
112
        }
113
114
        $attributes = ['id', 'name', 'handle', 'class'];
115
116
        foreach ($attributes as $attribute) {
117
            if (($value = $this->{$attribute}) !== null) {
118
                $this->andWhere(Db::parseParam(Provider::tableAlias() . '.' . $attribute, $value));
119
            }
120
        }
121
122
        $this->applyClientConditions();
123
        $this->applyInstanceConditions();
124
        $this->applyEnvironmentConditions();
125
    }
126
127
    /*******************************************
128
     * PARAMS
129
     *******************************************/
130
131
    /**
132
     * Apply environment params
133
     */
134
    protected function applyInstanceConditions()
135
    {
136
        $alias = ProviderInstance::tableAlias();
137
138
        $this->leftJoin(
139
            ProviderInstance::tableName() . ' ' . $alias,
140
            '[[' . $alias . '.providerId]] = [[' . Provider::tableAlias() . '.id]]'
141
        );
142
143
        $attributes = ['clientId', 'clientSecret'];
144
145
        foreach ($attributes as $attribute) {
146
            if (null !== ($value = $this->{$attribute})) {
147
                $this->andWhere(Db::parseParam($alias . '.' . $attribute, $value));
148
            }
149
        }
150
151
        $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...
152
    }
153
}
154