Completed
Push — develop ( 2c76d9...35dfd0 )
by Nate
09:57
created

DomainsQuery::prepare()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 14
cp 0
rs 8.8333
c 0
b 0
f 0
cc 7
nc 2
nop 1
crap 56
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
     * @param array $config
41
     * @return $this
42
     */
43
    public function configure(array $config)
44
    {
45
        QueryHelper::configure(
46
            $this,
47
            $config
48
        );
49
50
        return $this;
51
    }
52
53
    /**
54
     * @param $value
55
     * @return static
56
     */
57
    public function domain($value)
58
    {
59
        $this->domain = $value;
60
        return $this;
61
    }
62
63
    /**
64
     * @param $value
65
     * @return static
66
     */
67
    public function setDomain($value)
68
    {
69
        return $this->domain($value);
70
    }
71
72
    /**
73
     * @param $value
74
     * @return static
75
     */
76
    public function status($value)
77
    {
78
        $this->status = $value;
79
        return $this;
80
    }
81
82
    /**
83
     * @param $value
84
     * @return static
85
     */
86
    public function setStatus($value)
87
    {
88
        return $this->status($value);
89
    }
90
91
    /**
92
     * @inheritdoc
93
     *
94
     * @throws QueryAbortedException if it can be determined that there won’t be any results
95
     */
96
    public function prepare($builder)
97
    {
98
        // Is the query already doomed?
99
        if (($this->field !== null && empty($this->field)) ||
100
            ($this->domain !== null && empty($this->domain)) ||
101
            ($this->element !== null && empty($this->element))
102
        ) {
103
            throw new QueryAbortedException();
104
        }
105
106
        $this->applyConditions();
107
        $this->applySiteConditions();
108
        $this->applyFieldConditions();
109
        $this->applyElementConditions();
110
111
        return parent::prepare($builder);
112
    }
113
114
    /**
115
     * Apply attribute conditions
116
     */
117
    protected function applyConditions()
118
    {
119
        $attributes = ['domain', 'status'];
120
121
        foreach ($attributes as $attribute) {
122
            if (null !== ($value = $this->{$attribute})) {
123
                $this->andWhere(Db::parseParam($attribute, $value));
0 ignored issues
show
Bug introduced by
It seems like \craft\helpers\Db::parseParam($attribute, $value) targeting craft\helpers\Db::parseParam() can also be of type string; however, flipbox\craft\ember\quer...ActiveQuery::andWhere() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
124
            }
125
        }
126
    }
127
128
    /**
129
     *  Apply query specific conditions
130
     */
131
    protected function applyElementConditions()
132
    {
133
        if ($this->element !== null) {
134
            $this->andWhere(Db::parseParam('elementId', $this->parseElementValue($this->element)));
0 ignored issues
show
Bug introduced by
It seems like \craft\helpers\Db::parse...tValue($this->element)) targeting craft\helpers\Db::parseParam() can also be of type string; however, flipbox\craft\ember\quer...ActiveQuery::andWhere() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
135
        }
136
    }
137
138
    /**
139
     *  Apply query specific conditions
140
     */
141
    protected function applyFieldConditions()
142
    {
143
        if ($this->field !== null) {
144
            $this->andWhere(Db::parseParam('fieldId', $this->parseFieldValue($this->field)));
0 ignored issues
show
Bug introduced by
It seems like \craft\helpers\Db::parse...eldValue($this->field)) targeting craft\helpers\Db::parseParam() can also be of type string; however, flipbox\craft\ember\quer...ActiveQuery::andWhere() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
145
        }
146
    }
147
148
    /**
149
     *  Apply query specific conditions
150
     */
151
    protected function applySiteConditions()
152
    {
153
        if ($this->site !== null) {
154
            $this->andWhere(Db::parseParam('siteId', $this->parseSiteValue($this->site)));
0 ignored issues
show
Bug introduced by
It seems like \craft\helpers\Db::parse...SiteValue($this->site)) targeting craft\helpers\Db::parseParam() can also be of type string; however, flipbox\craft\ember\quer...ActiveQuery::andWhere() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
155
        }
156
    }
157
}
158