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

ProviderInstanceActiveQuery   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 7
dl 0
loc 81
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A id() 0 5 1
A init() 0 8 2
A fixedOrderColumn() 0 4 1
A prepare() 0 14 3
A applyProviderParam() 0 12 3
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/software/patron/
7
 */
8
9
namespace flipbox\patron\db;
10
11
use craft\db\QueryAbortedException;
12
use craft\helpers\Db;
13
use flipbox\ember\db\traits\AuditAttributes;
14
use flipbox\ember\db\traits\FixedOrderBy;
15
use flipbox\patron\records\ProviderInstance;
16
use yii\db\ActiveQuery;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
class ProviderInstanceActiveQuery extends ActiveQuery
23
{
24
    use traits\BaseProviderAttributes,
25
        FixedOrderBy,
26
        AuditAttributes;
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
     * @param $id
35
     * @return $this
36
     */
37
    public function id($id)
38
    {
39
        $this->id = $id;
40
        return $this;
41
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46
    public function init()
47
    {
48
        parent::init();
49
50
        if ($this->from === null) {
51
            $this->from = [ProviderInstance::tableName()];
52
        }
53
    }
54
55
    /*******************************************
56
     * FIXED ORDER
57
     *******************************************/
58
59
    /**
60
     * @inheritdoc
61
     */
62
    protected function fixedOrderColumn(): string
63
    {
64
        return 'accessToken';
65
    }
66
67
    /**
68
     * @inheritdoc
69
     *
70
     * @throws QueryAbortedException if it can be determined that there won’t be any results
71
     */
72
    public function prepare($builder)
73
    {
74
        // Is the query already doomed?
75
        if ($this->id !== null && empty($this->id)) {
76
            throw new QueryAbortedException();
77
        }
78
79
        $this->applyProviderParam();
80
        $this->applyEnvironmentParams();
81
        $this->applyAuditAttributeConditions();
82
        $this->applyOrderByParams($builder->db);
83
84
        return parent::prepare($builder);
85
    }
86
87
    /**
88
     * Apply environment params
89
     */
90
    protected function applyProviderParam()
91
    {
92
        $attributes = ['clientId', 'clientSecret'];
93
94
        foreach ($attributes as $attribute) {
95
            if (null !== ($value = $this->{$attribute})) {
96
                $this->andWhere(Db::parseParam(ProviderInstance::tableAlias() . '.' . $attribute, $value));
97
            }
98
        }
99
100
        $this->distinct(true);
101
    }
102
}
103