Completed
Push — develop ( f37ae4...1d947e )
by Nate
18:30
created

ResourceAssociationQuery::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/hubspot/license
6
 * @link       https://www.flipboxfactory.com/software/hubspot/
7
 */
8
9
namespace flipbox\hubspot\db;
10
11
use Craft;
12
use craft\db\QueryAbortedException;
13
use craft\helpers\Db;
14
use flipbox\hubspot\records\ResourceAssociation;
15
use flipbox\craft\sortable\associations\db\SortableAssociationQuery;
16
use flipbox\craft\sortable\associations\db\traits\SiteAttribute;
17
use flipbox\ember\db\traits\ElementAttribute;
18
19
/**
20
 * @method ResourceAssociation[] all()
21
 * @method ResourceAssociation one()
22
 */
23
class ResourceAssociationQuery extends SortableAssociationQuery
24
{
25
    use traits\FieldAttribute,
26
        traits\HubSpotIdAttribute,
27
        ElementAttribute,
28
        SiteAttribute;
29
30
    /**
31
     * @inheritdoc
32
     */
33
    protected function fixedOrderColumn(): string
34
    {
35
        return 'objectId';
36
    }
37
38
    /**
39
     * @param array $config
40
     * @return $this
41
     */
42
    public function configure(array $config)
43
    {
44
        return Craft::configure(
45
            $this,
46
            $config
47
        );
48
    }
49
50
    /**
51
     * @inheritdoc
52
     *
53
     * @throws QueryAbortedException if it can be determined that there won’t be any results
54
     */
55
    public function prepare($builder)
56
    {
57
        // Is the query already doomed?
58
        if (($this->field !== null && empty($this->field)) ||
59
            ($this->hubSpotId !== null && empty($this->hubSpotId)) ||
60
            ($this->element !== null && empty($this->element))
61
        ) {
62
            throw new QueryAbortedException();
63
        }
64
65
        $this->applyConditions();
66
        $this->applySiteConditions();
67
        $this->applyHubSpotIdConditions();
68
        $this->applyFieldConditions();
69
70
        return parent::prepare($builder);
71
    }
72
73
    /**
74
     *  Apply query specific conditions
75
     */
76
    protected function applyConditions()
77
    {
78
        if ($this->element !== null) {
79
            $this->andWhere(Db::parseParam('elementId', $this->parseElementValue($this->element)));
80
        }
81
    }
82
}
83