Completed
Push — master ( 11971b...eeafb3 )
by Nate
01:16
created

ActiveQuery   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 3
dl 0
loc 152
ccs 0
cts 91
cp 0
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A andWhere() 0 13 4
A requireAll() 0 14 2
A requireOne() 0 12 2
A all() 0 8 2
A one() 0 12 2
A scalar() 0 12 2
A column() 0 8 2
A exists() 0 8 2
A queryScalar() 0 8 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember/
7
 */
8
9
namespace flipbox\craft\ember\queries;
10
11
use craft\base\ClonefixTrait;
12
use craft\db\QueryAbortedException;
13
use flipbox\craft\ember\exceptions\RecordNotFoundException;
14
use flipbox\craft\ember\records\ActiveRecord;
15
use yii\db\Connection;
16
17
/**
18
 * This class provides Element Query like functionality to Yii's ActiveQuery.  Primarily,
19
 * the query can throw a `QueryAbortedException` which is caught and handled appropriately.
20
 *
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 2.0.0
23
 */
24
class ActiveQuery extends \yii\db\ActiveQuery
25
{
26
    use ClonefixTrait;
27
28
    /**
29
     * Attempt to set query properties/methods first, otherwise
30
     * perform standard andWhere logic as expected.
31
     *
32
     * @inheritdoc
33
     */
34
    public function andWhere($condition, $params = [])
35
    {
36
        if (is_array($condition)) {
37
            foreach ($condition as $key => $value) {
38
                if ($this->canSetProperty($key)) {
39
                    $this->{$key} = $value;
40
                    unset($condition[$key]);
41
                }
42
            }
43
        }
44
45
        return parent::andWhere($condition);
46
    }
47
48
    /**
49
     * Executes query and returns all results as an array.  If results are not found, an exception is
50
     * thrown as we explicitly expect results.
51
     *
52
     * @param Connection $db the DB connection used to create the DB command.
53
     * If null, the DB connection returned by [[modelClass]] will be used.
54
     * @return array|ActiveRecord[] the query results. If the query results in nothing, an empty array will be returned.
55
     * @throws RecordNotFoundException
56
     */
57
    public function requireAll($db = null)
58
    {
59
        $records = $this->all($db);
60
61
        if (empty($records)) {
62
            throw new RecordNotFoundException(
63
                sprintf(
64
                    "Records not found."
65
                )
66
            );
67
        }
68
69
        return $records;
70
    }
71
72
    /**
73
     * Executes query and returns a single result.  If a result is not found, an exception is
74
     * thrown as we explicitly expect a result.
75
     *
76
     * @param Connection|null $db the DB connection used to create the DB command.
77
     * If `null`, the DB connection returned by [[modelClass]] will be used.
78
     * @return ActiveRecord|array a single row of query result. Depending on the setting of [[asArray]],
79
     * the query result may be either an array or an ActiveRecord object. `null` will be returned
80
     * if the query results in nothing.
81
     * @throws RecordNotFoundException
82
     */
83
    public function requireOne($db = null)
84
    {
85
        if (null === ($record = $this->one($db))) {
86
            throw new RecordNotFoundException(
87
                sprintf(
88
                    "Record not found."
89
                )
90
            );
91
        }
92
93
        return $record;
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99
    public function all($db = null)
100
    {
101
        try {
102
            return parent::all($db);
103
        } /** @noinspection PhpRedundantCatchClauseInspection */ catch (QueryAbortedException $e) {
104
            return [];
105
        }
106
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111
    public function one($db = null)
112
    {
113
        $limit = $this->limit;
114
        $this->limit = 1;
115
        try {
116
            $result = parent::one($db);
117
        } /** @noinspection PhpRedundantCatchClauseInspection */ catch (QueryAbortedException $e) {
118
            $result = null;
119
        }
120
        $this->limit = $limit;
121
        return $result;
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127
    public function scalar($db = null)
128
    {
129
        $limit = $this->limit;
130
        $this->limit = 1;
131
        try {
132
            $result = parent::scalar($db);
133
        } /** @noinspection PhpRedundantCatchClauseInspection */ catch (QueryAbortedException $e) {
134
            $result = false;
135
        }
136
        $this->limit = $limit;
137
        return $result;
138
    }
139
140
    /**
141
     * @inheritdoc
142
     */
143
    public function column($db = null)
144
    {
145
        try {
146
            return parent::column($db);
147
        } /** @noinspection PhpRedundantCatchClauseInspection */ catch (QueryAbortedException $e) {
148
            return [];
149
        }
150
    }
151
152
    /**
153
     * @inheritdoc
154
     */
155
    public function exists($db = null)
156
    {
157
        try {
158
            return parent::exists($db);
159
        } /** @noinspection PhpRedundantCatchClauseInspection */ catch (QueryAbortedException $e) {
160
            return false;
161
        }
162
    }
163
164
    /**
165
     * @inheritdoc
166
     */
167
    protected function queryScalar($selectExpression, $db)
168
    {
169
        try {
170
            return parent::queryScalar($selectExpression, $db);
171
        } /** @noinspection PhpRedundantCatchClauseInspection */ catch (QueryAbortedException $e) {
172
            return false;
173
        }
174
    }
175
}
176