DomainsQuery   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 7
dl 0
loc 142
ccs 0
cts 67
cp 0
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 9 1
A domain() 0 5 1
A setDomain() 0 4 1
A status() 0 5 1
A setStatus() 0 4 1
B prepare() 0 17 7
A applyConditions() 0 10 3
A applyElementConditions() 0 6 2
A applyFieldConditions() 0 6 2
A applySiteConditions() 0 6 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/domains/license
6
 * @link       https://www.flipboxfactory.com/software/domains/
7
 */
8
9
namespace flipbox\craft\domains\queries;
10
11
use craft\db\QueryAbortedException;
12
use craft\helpers\Db;
13
use flipbox\craft\ember\helpers\QueryHelper;
14
use flipbox\craft\ember\queries\CacheableActiveQuery;
15
use flipbox\craft\ember\queries\ElementAttributeTrait;
16
use flipbox\craft\ember\queries\FieldAttributeTrait;
17
use flipbox\craft\ember\queries\SiteAttributeTrait;
18
use flipbox\craft\domains\records\Domain;
19
20
/**
21
 * @method Domain[] getCachedResult()
22
 */
23
class DomainsQuery extends CacheableActiveQuery
24
{
25
    use FieldAttributeTrait,
26
        ElementAttributeTrait,
27
        SiteAttributeTrait;
28
29
    /**
30
     * @var string|string[]|false|null The domain(s). Prefix with "not " to exclude them.
31
     */
32
    public $domain;
33
34
    /**
35
     * @var string|string[]|false|null The status(es). Prefix with "not " to exclude them.
36
     */
37
    public $status;
38
39
    /**
40
     * @var array
41
     */
42
    public $orderBy = [
43
        'sortOrder' => SORT_ASC
44
    ];
45
46
    /**
47
     * @param array $config
48
     * @return $this
49
     */
50
    public function configure(array $config)
51
    {
52
        QueryHelper::configure(
53
            $this,
54
            $config
55
        );
56
57
        return $this;
58
    }
59
60
    /**
61
     * @param $value
62
     * @return static
63
     */
64
    public function domain($value)
65
    {
66
        $this->domain = $value;
67
        return $this;
68
    }
69
70
    /**
71
     * @param $value
72
     * @return static
73
     */
74
    public function setDomain($value)
75
    {
76
        return $this->domain($value);
77
    }
78
79
    /**
80
     * @param $value
81
     * @return static
82
     */
83
    public function status($value)
84
    {
85
        $this->status = $value;
86
        return $this;
87
    }
88
89
    /**
90
     * @param $value
91
     * @return static
92
     */
93
    public function setStatus($value)
94
    {
95
        return $this->status($value);
96
    }
97
98
    /**
99
     * @inheritdoc
100
     *
101
     * @throws QueryAbortedException if it can be determined that there won’t be any results
102
     */
103
    public function prepare($builder)
104
    {
105
        // Is the query already doomed?
106
        if (($this->field !== null && empty($this->field)) ||
107
            ($this->domain !== null && empty($this->domain)) ||
108
            ($this->element !== null && empty($this->element))
109
        ) {
110
            throw new QueryAbortedException();
111
        }
112
113
        $this->applyConditions();
114
        $this->applySiteConditions();
115
        $this->applyFieldConditions();
116
        $this->applyElementConditions();
117
118
        return parent::prepare($builder);
119
    }
120
121
    /**
122
     * Apply attribute conditions
123
     */
124
    protected function applyConditions()
125
    {
126
        $attributes = ['domain', 'status'];
127
128
        foreach ($attributes as $attribute) {
129
            if (null !== ($value = $this->{$attribute})) {
130
                $this->andWhere(Db::parseParam($attribute, $value));
131
            }
132
        }
133
    }
134
135
    /**
136
     *  Apply query specific conditions
137
     */
138
    protected function applyElementConditions()
139
    {
140
        if ($this->element !== null) {
141
            $this->andWhere(Db::parseParam('elementId', $this->parseElementValue($this->element)));
142
        }
143
    }
144
145
    /**
146
     *  Apply query specific conditions
147
     */
148
    protected function applyFieldConditions()
149
    {
150
        if ($this->field !== null) {
151
            $this->andWhere(Db::parseParam('fieldId', $this->parseFieldValue($this->field)));
152
        }
153
    }
154
155
    /**
156
     *  Apply query specific conditions
157
     */
158
    protected function applySiteConditions()
159
    {
160
        if ($this->site !== null) {
161
            $this->andWhere(Db::parseParam('siteId', $this->parseSiteValue($this->site)));
162
        }
163
    }
164
}
165