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

ProviderInstanceActiveQuery::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 10
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/software/patron/
7
 */
8
9
namespace flipbox\patron\queries;
10
11
use craft\db\QueryAbortedException;
12
use craft\helpers\Db;
13
use flipbox\craft\ember\models\AuditAttributesTrait;
14
use flipbox\patron\records\ProviderInstance;
15
use yii\db\ActiveQuery;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
class ProviderInstanceActiveQuery extends ActiveQuery
22
{
23
    use ProviderClientAttributesTrait,
24
        ProviderEnvironmentAttributesTrait,
25
        AuditAttributesTrait;
26
27
    /**
28
     * @var int|int[]|false|null The model ID(s). Prefix IDs with "not " to exclude them.
29
     */
30
    public $id;
31
32
    /**
33
     * @param $id
34
     * @return $this
35
     */
36
    public function id($id)
37
    {
38
        $this->id = $id;
39
        return $this;
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function init()
46
    {
47
        parent::init();
48
49
        if ($this->from === null) {
50
            $this->from = [ProviderInstance::tableName()];
51
        }
52
    }
53
54
55
    /**
56
     * @inheritdoc
57
     *
58
     * @throws QueryAbortedException if it can be determined that there won’t be any results
59
     */
60
    public function prepare($builder)
61
    {
62
        // Is the query already doomed?
63
        if ($this->id !== null && empty($this->id)) {
64
            throw new QueryAbortedException();
65
        }
66
67
        $this->applyClientConditions();
68
        $this->applyProviderParam();
69
        $this->applyEnvironmentConditions();
70
71
        return parent::prepare($builder);
72
    }
73
74
    /**
75
     * Apply environment params
76
     */
77
    protected function applyProviderParam()
78
    {
79
        $attributes = ['id'];
80
81
        foreach ($attributes as $attribute) {
82
            if (null !== ($value = $this->{$attribute})) {
83
                $this->andWhere(Db::parseParam(ProviderInstance::tableAlias() . '.' . $attribute, $value));
84
            }
85
        }
86
87
        $this->distinct(true);
88
    }
89
}
90