Completed
Push — master ( faa25a...6f52ce )
by Nate
01:16
created

DomainsQuery   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
lcom 3
cbo 6
dl 0
loc 124
ccs 0
cts 53
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A init() 0 17 3
A getField() 0 4 1
A fixedOrderColumn() 0 4 1
A fixedOrder() 0 6 1
A prepare() 0 15 3
A one() 0 10 2
A createObject() 0 4 1
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\domains\db;
10
11
use craft\db\QueryAbortedException;
12
use flipbox\domains\Domains as DomainsPlugin;
13
use flipbox\domains\fields\Domains;
14
use flipbox\domains\models\Domain;
15
use flipbox\ember\db\CacheableQuery;
16
use flipbox\ember\db\traits\AuditAttributes;
17
use flipbox\ember\db\traits\FixedOrderBy;
18
use flipbox\ember\db\traits\PopulateObject;
19
use yii\base\ArrayableTrait;
20
21
class DomainsQuery extends CacheableQuery
0 ignored issues
show
Bug introduced by
There is one abstract method andWhere in this class; you could implement it, or declare this class as abstract.
Loading history...
22
{
23
    use ArrayableTrait, PopulateObject, traits\Attributes, AuditAttributes, FixedOrderBy;
24
25
    /**
26
     * @var bool Whether results should be returned in the order specified by [[domain]].
27
     */
28
    public $fixedOrder = false;
29
30
    /**
31
     * @inheritdoc
32
     */
33
    public $orderBy = 'sortOrder';
34
35
    /**
36
     * @var Domains
37
     */
38
    private $field;
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function __construct(Domains $domains, $config = [])
44
    {
45
        $this->field = $domains;
46
        parent::__construct($config);
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function init()
53
    {
54
        parent::init();
55
56
        if ($this->select === null) {
57
            // Use ** as a placeholder for "all the default columns"
58
            $this->select = ['*'];
59
        }
60
61
        // Set table name
62
        if ($this->from === null) {
63
            $fieldService = DomainsPlugin::getInstance()->getField();
64
            $this->from([
65
                $fieldService->getTableName($this->field) . ' ' . $fieldService->getTableAlias($this->field)
66
            ]);
67
        }
68
    }
69
70
    /**
71
     * @return Domains
72
     */
73
    public function getField(): Domains
74
    {
75
        return $this->field;
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81
    protected function fixedOrderColumn(): string
82
    {
83
        return 'domain';
84
    }
85
86
    /**
87
     * @inheritdoc
88
     * return static
89
     */
90
    public function fixedOrder(bool $value = true)
91
    {
92
        $this->fixedOrder = $value;
93
94
        return $this;
95
    }
96
97
    // Query preparation/execution
98
    // -------------------------------------------------------------------------
99
100
    /**
101
     * @inheritdoc
102
     *
103
     * @throws QueryAbortedException if it can be determined that there won’t be any results
104
     */
105
    public function prepare($builder)
106
    {
107
        // Is the query already doomed?
108
        if ($this->domain !== null && empty($this->domain)) {
109
            throw new QueryAbortedException();
110
        }
111
112
        // Build the query
113
        // ---------------------------------------------------------------------
114
        $this->applyConditions();
115
        $this->applyAuditAttributeConditions();
116
        $this->applyOrderByParams($builder->db);
117
118
        return parent::prepare($builder);
119
    }
120
121
    /**
122
     * @inheritdoc
123
     */
124
    public function one($db = null)
125
    {
126
        $row = parent::one($db);
127
128
        if ($row === false) {
129
            return false;
130
        }
131
132
        return $this->createObject($row);
133
    }
134
135
    /**
136
     * @param $row
137
     *
138
     * @return Domain
139
     */
140
    protected function createObject($row): Domain
141
    {
142
        return new Domain($this->getField(), $row);
143
    }
144
}
145