Completed
Push — master ( 4395fa...7779bb )
by Nate
12:11 queued 10:17
created

Accessor::buildQueryFromCondition()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 17
cp 0
rs 9.2568
c 0
b 0
f 0
cc 5
nc 2
nop 1
crap 30
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\ember\services\traits\queries;
10
11
use flipbox\ember\exceptions\NotFoundException;
12
use flipbox\ember\helpers\QueryHelper;
13
use flipbox\ember\helpers\RecordHelper;
14
use yii\base\BaseObject;
15
use yii\db\QueryInterface;
16
17
/**
18
 * A set of robust methods commonly used to retrieve data from the attached database.  An optional
19
 * cache layer can be applied to circumvent heavy queries.
20
 *
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 */
24
trait Accessor
25
{
26
    use BaseAccessor;
27
28
    /*******************************************
29
     * FIND / GET
30
     *******************************************/
31
32
    /**
33
     * @return array[]
34
     */
35
    public function findAll()
36
    {
37
        return $this->findAllByCondition([]);
38
    }
39
40
    /**
41
     * @param $identifier
42
     * @return mixed|null
43
     */
44
    public function find($identifier)
45
    {
46
        return $this->findByCondition($identifier);
47
    }
48
49
    /**
50
     * @param $identifier
51
     * @return mixed
52
     * @throws NotFoundException
53
     */
54
    public function get($identifier)
55
    {
56
        if (null === ($object = $this->find($identifier))) {
57
            $this->notFoundException();
58
        }
59
60
        return $object;
61
    }
62
63
64
    /*******************************************
65
     * ONE CONDITION
66
     *******************************************/
67
68
    /**
69
     * @param $condition
70
     * @return mixed|null
71
     */
72
    public function findByCondition($condition)
73
    {
74
        return $this->queryOne(
75
            $this->buildQueryFromCondition($condition)
76
        );
77
    }
78
79
    /**
80
     * @param $condition
81
     * @return mixed
82
     * @throws NotFoundException
83
     */
84
    public function getByCondition($condition)
85
    {
86
        if (null === ($object = $this->findByCondition($condition))) {
87
            $this->notFoundException();
88
        }
89
90
        return $object;
91
    }
92
93
94
    /*******************************************
95
     * ONE CRITERIA
96
     *******************************************/
97
98
    /**
99
     * @param $criteria
100
     * @return mixed|null
101
     */
102
    public function findByCriteria($criteria)
103
    {
104
        $object = $this->queryOne(
105
            $this->getQuery($criteria)
106
        );
107
108
        return $object;
109
    }
110
111
    /**
112
     * @param $criteria
113
     * @return mixed
114
     * @throws NotFoundException
115
     */
116
    public function getByCriteria($criteria)
117
    {
118
        if (null === ($record = $this->findByCriteria($criteria))) {
119
            $this->notFoundException();
120
        }
121
122
        return $record;
123
    }
124
125
126
    /*******************************************
127
     * ALL CONDITION
128
     *******************************************/
129
130
    /**
131
     * @param array $condition
132
     * @return array
133
     */
134
    public function findAllByCondition($condition = []): array
135
    {
136
        return $this->queryAll(
137
            $this->buildQueryFromCondition($condition)
138
        );
139
    }
140
141
    /**
142
     * @param array $condition
143
     * @return array
144
     * @throws NotFoundException
145
     */
146
    public function getAllByCondition($condition = []): array
147
    {
148
        $records = $this->findAllByCondition($condition);
149
        if (empty($records)) {
150
            $this->notFoundException();
151
        }
152
153
        return $records;
154
    }
155
156
    /*******************************************
157
     * ALL CRITERIA
158
     *******************************************/
159
160
    /**
161
     * @param array $criteria
162
     * @return array
163
     */
164
    public function findAllByCriteria($criteria = []): array
165
    {
166
        return $this->queryAll(
167
            $this->getQuery($criteria)
168
        );
169
    }
170
171
    /**
172
     * @param array $criteria
173
     * @return array
174
     * @throws NotFoundException
175
     */
176
    public function getAllByCriteria($criteria = []): array
177
    {
178
        $records = $this->findAllByCriteria($criteria);
179
        if (empty($records)) {
180
            $this->notFoundException();
181
        }
182
183
        return $records;
184
    }
185
186
    /**
187
     * @param array $condition
188
     * @return QueryInterface
189
     */
190
    protected function buildQueryFromCondition($condition = []): QueryInterface
191
    {
192
        /** @var QueryInterface $query */
193
        $query = $this->getQuery();
194
195
        // Apply method/property vs setting in 'where'
196
        if ($query instanceof BaseObject && is_array($condition)) {
197
            foreach ($condition as $key => $value) {
198
                if ($query->canSetProperty($key)) {
199
                    $query->{$key} = $value;
200
                    unset($condition[$key]);
201
                }
202
            }
203
        }
204
205
        QueryHelper::configure(
206
            $query,
207
            RecordHelper::conditionToCriteria($condition)
208
        );
209
210
        return $query;
211
    }
212
}
213