Completed
Push — master ( de0c31...4a4246 )
by Nate
06:58
created

Accessor::queryOne()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 0
cts 15
cp 0
rs 9.4285
cc 3
eloc 11
nc 5
nop 1
crap 12
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\RecordHelper;
13
14
/**
15
 * A set of robust methods commonly used to retrieve data from the attached database.  An optional
16
 * cache layer can be applied to circumvent heavy queries.
17
 *
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
trait Accessor
22
{
23
    use BaseAccessor;
24
25
    /*******************************************
26
     * FIND / GET
27
     *******************************************/
28
29
    /**
30
     * @return array[]
31
     */
32
    public function findAll()
33
    {
34
        return $this->findAllByCondition(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
35
    }
36
37
    /**
38
     * @param $identifier
39
     * @return mixed|null
40
     */
41
    public function find($identifier)
42
    {
43
        return $this->findByCondition($identifier);
44
    }
45
46
    /**
47
     * @param $identifier
48
     * @return mixed
49
     * @throws NotFoundException
50
     */
51
    public function get($identifier)
52
    {
53
        if (null === ($object = $this->find($identifier))) {
54
            $this->notFoundException();
55
        }
56
57
        return $object;
58
    }
59
60
61
    /*******************************************
62
     * ONE CONDITION
63
     *******************************************/
64
65
    /**
66
     * @param $condition
67
     * @return mixed|null
68
     */
69
    public function findByCondition($condition)
70
    {
71
        return $this->findByCriteria(
72
            RecordHelper::conditionToCriteria($condition)
73
        );
74
    }
75
76
    /**
77
     * @param $condition
78
     * @return mixed
79
     * @throws NotFoundException
80
     */
81
    public function getByCondition($condition)
82
    {
83
        if (null === ($object = $this->findByCondition($condition))) {
84
            $this->notFoundException();
85
        }
86
87
        return $object;
88
    }
89
90
91
    /*******************************************
92
     * ONE CRITERIA
93
     *******************************************/
94
95
    /**
96
     * @param $criteria
97
     * @return mixed|null
98
     */
99
    public function findByCriteria($criteria)
100
    {
101
        $object = $this->queryOne(
102
            $this->getQuery($criteria)
103
        );
104
105
        return $object;
106
    }
107
108
    /**
109
     * @param $criteria
110
     * @return mixed
111
     * @throws NotFoundException
112
     */
113
    public function getByCriteria($criteria)
114
    {
115
        if (null === ($record = $this->findByCriteria($criteria))) {
116
            $this->notFoundException();
117
        }
118
119
        return $record;
120
    }
121
122
123
    /*******************************************
124
     * ALL CONDITION
125
     *******************************************/
126
127
    /**
128
     * @param array $condition
129
     * @return array
130
     */
131
    public function findAllByCondition($condition = []): array
132
    {
133
        return $this->findAllByCriteria(
134
            RecordHelper::conditionToCriteria($condition)
135
        );
136
    }
137
138
    /**
139
     * @param array $condition
140
     * @return array
141
     * @throws NotFoundException
142
     */
143
    public function getAllByCondition($condition = []): array
144
    {
145
        $records = $this->findAllByCondition($condition);
146
        if (empty($records)) {
147
            $this->notFoundException();
148
        }
149
150
        return $records;
151
    }
152
153
    /*******************************************
154
     * ALL CRITERIA
155
     *******************************************/
156
157
    /**
158
     * @param array $criteria
159
     * @return array
160
     */
161
    public function findAllByCriteria($criteria = []): array
162
    {
163
        $records = $this->queryAll(
164
            $this->getQuery($criteria)
165
        );
166
167
        return $records;
168
    }
169
170
    /**
171
     * @param array $criteria
172
     * @return array
173
     * @throws NotFoundException
174
     */
175
    public function getAllByCriteria($criteria = []): array
176
    {
177
        $records = $this->findAllByCriteria($criteria);
178
        if (empty($records)) {
179
            $this->notFoundException();
180
        }
181
182
        return $records;
183
    }
184
}
185