Completed
Push — develop ( edb8bf...6f43a0 )
by Nate
08:26
created

ProviderAttributesTrait::applyProviderConditions()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.9297
c 0
b 0
f 0
cc 6
nc 7
nop 0
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
     * @param $enabled
47
     * @return $this
48
     */
49
    public function enabled($enabled)
50
    {
51
        $this->enabled = $enabled;
52
        return $this;
53
    }
54
55
    /**
56
     * @param $id
57
     * @return $this
58
     */
59
    public function id($id)
60
    {
61
        $this->id = $id;
62
        return $this;
63
    }
64
65
    /**
66
     * @param $name
67
     * @return $this
68
     */
69
    public function name($name)
70
    {
71
        $this->name = $name;
72
        return $this;
73
    }
74
75
    /**
76
     * @param $handle
77
     * @return $this
78
     */
79
    public function handle($handle)
80
    {
81
        $this->handle = $handle;
82
        return $this;
83
    }
84
85
    /**
86
     * @throws QueryAbortedException
87
     */
88
    protected function applyProviderConditions()
89
    {
90
        // Is the query already doomed?
91
        if ($this->id !== null && empty($this->id)) {
92
            throw new QueryAbortedException();
93
        }
94
95
        if ($this->enabled !== null) {
96
            $this->andWhere(Db::parseParam('enabled', $this->enabled));
97
        }
98
99
        $attributes = ['id', 'name', 'handle'];
100
101
        foreach ($attributes as $attribute) {
102
            if (($value = $this->{$attribute}) !== null) {
103
                $this->andWhere(Db::parseParam(Provider::tableAlias() . '.' . $attribute, $value));
104
            }
105
        }
106
107
        $this->applyClientConditions();
108
        $this->applyInstanceConditions();
109
        $this->applyEnvironmentConditions();
110
    }
111
112
    /*******************************************
113
     * PARAMS
114
     *******************************************/
115
116
    /**
117
     * Apply environment params
118
     */
119
    protected function applyInstanceConditions()
120
    {
121
        $alias = ProviderInstance::tableAlias();
122
123
        $this->leftJoin(
124
            ProviderInstance::tableName() . ' ' . $alias,
125
            '[[' . $alias . '.providerId]] = [[' . Provider::tableAlias() . '.id]]'
126
        );
127
128
        $attributes = ['clientId', 'clientSecret'];
129
130
        foreach ($attributes as $attribute) {
131
            if (null !== ($value = $this->{$attribute})) {
132
                $this->andWhere(Db::parseParam($alias . '.' . $attribute, $value));
133
            }
134
        }
135
136
        $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...
137
    }
138
}
139