Completed
Push — develop ( 0ff0ed...a13565 )
by Nate
12:12
created

IntegrationAssociationQuery::applySiteConditions()   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
c 0
b 0
f 0
ccs 0
cts 8
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-sortable-associations/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-sortable-associations
7
 */
8
9
namespace flipbox\craft\integration\queries;
10
11
use Craft;
12
use craft\db\QueryAbortedException;
13
use craft\helpers\Db;
14
use flipbox\craft\ember\queries\AuditAttributesTrait;
15
use flipbox\craft\ember\queries\CacheableActiveQuery;
16
use flipbox\craft\ember\queries\ElementAttributeTrait;
17
use flipbox\craft\ember\queries\FieldAttributeTrait;
18
use flipbox\craft\ember\queries\SiteAttributeTrait;
19
use flipbox\craft\integration\records\IntegrationAssociation;
20
21
/**
22
 * @method IntegrationAssociation[] getCachedResult()
23
 * @method IntegrationAssociation[] all()
24
 * @method IntegrationAssociation one()
25
 */
26
class IntegrationAssociationQuery extends CacheableActiveQuery
27
{
28
    use AuditAttributesTrait,
29
        FieldAttributeTrait,
30
        ElementAttributeTrait,
31
        ObjectAttributeTrait,
32
        SiteAttributeTrait;
33
34
    /**
35
     * The sort order attribute
36
     */
37
    const SORT_ORDER_ATTRIBUTE = 'sortOrder';
38
39
    /**
40
     * The sort order direction
41
     */
42
    const SORT_ORDER_DIRECTION = SORT_ASC;
43
44
    /**
45
     * @var int|null Sort order
46
     */
47
    public $sortOrder;
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function init()
53
    {
54
        parent::init();
55
56
        if ($this->select === null) {
57
            $this->select = ['*'];
58
        }
59
60
        if ($this->orderBy === null && static::SORT_ORDER_ATTRIBUTE !== null) {
61
            $this->orderBy = [static::SORT_ORDER_ATTRIBUTE => static::SORT_ORDER_DIRECTION];
62
        }
63
    }
64
65
    /**
66
     * @param $value
67
     * @return $this
68
     */
69
    public function sortOrder($value)
70
    {
71
        $this->sortOrder = $value;
72
        return $this;
73
    }
74
75
    /**
76
     * @param $value
77
     * @return $this
78
     */
79
    public function setSortOrder($value)
80
    {
81
        return $this->sortOrder($value);
82
    }
83
84
    /**
85
     * @inheritdoc
86
     * @throws QueryAbortedException
87
     */
88
    public function prepare($builder)
89
    {
90
        // Is the query already doomed?
91
        if (($this->field !== null && empty($this->field)) ||
92
            ($this->object !== null && empty($this->object)) ||
93
            ($this->element !== null && empty($this->element))
94
        ) {
95
            throw new QueryAbortedException();
96
        }
97
98
        if ($this->sortOrder !== null) {
99
            $this->andWhere(Db::parseParam(static::SORT_ORDER_ATTRIBUTE, $this->sortOrder));
0 ignored issues
show
Bug introduced by
It seems like \craft\helpers\Db::parse...BUTE, $this->sortOrder) 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...
100
        }
101
102
        $this->applyElementConditions();
103
        $this->applyFieldConditions();
104
        $this->applyObjectConditions();
105
        $this->applySiteConditions();
106
        $this->applyAuditAttributeConditions();
107
108
        return parent::prepare($builder);
109
    }
110
111
    /**
112
     * Apply attribute conditions
113
     */
114
    protected function applyElementConditions()
115
    {
116
        if ($this->element !== null) {
117
            $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...
118
        }
119
    }
120
121
    /**
122
     * Apply attribute conditions
123
     */
124
    protected function applyFieldConditions()
125
    {
126
        if ($this->field !== null) {
127
            $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...
128
        }
129
    }
130
131
    /**
132
     * Apply attribute conditions
133
     */
134
    protected function applySiteConditions()
135
    {
136
        if ($this->site !== null) {
137
            $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...
138
        } else {
139
            $this->andWhere(Db::parseParam('siteId', Craft::$app->getSites()->currentSite->id));
0 ignored issues
show
Bug introduced by
It seems like \craft\helpers\Db::parse...tes()->currentSite->id) 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...
140
        }
141
    }
142
}
143