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

BaseAccessor::getByQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
nc 2
cc 2
eloc 4
nop 1
crap 6
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 Craft;
12
use flipbox\ember\exceptions\NotFoundException;
13
use yii\caching\Dependency;
14
use yii\db\Connection;
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 BaseAccessor
25
{
26
    /*******************************************
27
     * QUERY
28
     *******************************************/
29
30
    /**
31
     * @param array $config
32
     * @return \yii\db\ActiveQuery
33
     */
34
    abstract public function getQuery($config = []): QueryInterface;
35
36
    /*******************************************
37
     * CACHE
38
     *******************************************/
39
40
    /**
41
     * @return int|null
42
     */
43
    protected static function cacheDuration()
44
    {
45
        return false;
46
    }
47
48
    /**
49
     * @return null|Dependency
50
     */
51
    protected static function cacheDependency()
52
    {
53
        return null;
54
    }
55
56
    /**
57
     * @return Connection
58
     */
59
    protected static function getDb(): Connection
60
    {
61
        return Craft::$app->getDb();
62
    }
63
64
    /*******************************************
65
     * ONE QUERY
66
     *******************************************/
67
68
    /**
69
     * @param QueryInterface $query
70
     * @return mixed|null
71
     */
72
    public function findByQuery(QueryInterface $query)
73
    {
74
        return $this->queryOne($query);
75
    }
76
77
    /**
78
     * @param QueryInterface $query
79
     * @return mixed
80
     * @throws NotFoundException
81
     */
82
    public function getByQuery(QueryInterface $query)
83
    {
84
        if (null === ($object = $this->findByQuery($query))) {
85
            $this->notFoundException();
86
        }
87
88
        return $object;
89
    }
90
91
    /*******************************************
92
     * ALL BY QUERY
93
     *******************************************/
94
95
    /**
96
     * @param QueryInterface $query
97
     * @return array
98
     */
99
    public function findAllByQuery(QueryInterface $query): array
100
    {
101
        return $this->queryAll($query);
102
    }
103
104
    /**
105
     * @param QueryInterface $query
106
     * @return array
107
     * @throws NotFoundException
108
     */
109
    public function getAllByQuery(QueryInterface $query): array
110
    {
111
        $records = $this->findAllByQuery($query);
112
        if (empty($records)) {
113
            $this->notFoundException();
114
        }
115
116
        return $records;
117
    }
118
119
120
    /*******************************************
121
     * CACHE
122
     *******************************************/
123
124
    /**
125
     * @param QueryInterface $query
126
     * @return mixed|null
127
     */
128
    protected function queryOne(QueryInterface $query)
129
    {
130
        $db = static::getDb();
131
132
        try {
133
            if (false === ($cacheDuration = static::cacheDuration())) {
134
                return $query->one($db);
135
            }
136
137
            $result = $db->cache(function ($db) use ($query) {
138
                return $query->one($db);
139
            }, $cacheDuration, static::cacheDependency());
0 ignored issues
show
Documentation introduced by
$cacheDuration is of type boolean, but the function expects a integer|null.

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...
140
        } catch (\Exception $e) {
141
            return null;
142
        }
143
144
        return $result;
145
    }
146
147
    /**
148
     *
149
     * @param QueryInterface $query
150
     * @return mixed[]
151
     */
152
    protected function queryAll(QueryInterface $query)
153
    {
154
        $db = static::getDb();
155
156
        try {
157
            if (false === ($cacheDuration = static::cacheDuration())) {
158
                return $query->all($db);
159
            }
160
161
            $results = $db->cache(function ($db) use ($query) {
162
                return $query->all($db);
163
            }, $cacheDuration, static::cacheDependency());
0 ignored issues
show
Documentation introduced by
$cacheDuration is of type boolean, but the function expects a integer|null.

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...
164
        } catch (\Exception $e) {
165
            return [];
166
        }
167
168
        return $results;
169
    }
170
171
    /*******************************************
172
     * EXCEPTIONS
173
     *******************************************/
174
175
    /**
176
     * @throws NotFoundException
177
     */
178
    protected function notFoundException()
179
    {
180
        throw new NotFoundException(
181
            sprintf(
182
                "Results not found."
183
            )
184
        );
185
    }
186
}
187