Completed
Push — develop ( dd66f1...2b08f3 )
by Nate
04:16
created

BaseAccessor::getDb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 0
crap 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\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
     * @var int|null|false
28
     */
29
    protected $cacheDuration = false;
30
31
    /**
32
     * @var null|Dependency
33
     */
34
    protected $cacheDependency = null;
35
36
    /*******************************************
37
     * QUERY
38
     *******************************************/
39
40
    /**
41
     * @param array $config
42
     * @return \yii\db\ActiveQuery
43
     */
44
    abstract public function getQuery($config = []): QueryInterface;
45
46
    /*******************************************
47
     * CACHE
48
     *******************************************/
49
50
    /**
51
     * @param $duration
52
     * @return $this
53
     */
54
    public function setCacheDuration($duration)
55
    {
56
        if (is_numeric($duration)) {
57
            $duration = (int)$duration;
58
        }
59
60
        $this->cacheDuration = $duration;
61
        return $this;
62
    }
63
64
    /**
65
     * @param Dependency|null $dependency
66
     * @return $this
67
     */
68
    public function setCacheDependency(Dependency $dependency = null)
69
    {
70
        $this->cacheDependency = $dependency;
71
        return $this;
72
    }
73
74
    /**
75
     * @return Connection
76
     */
77
    protected static function getDb(): Connection
78
    {
79
        return Craft::$app->getDb();
80
    }
81
82
    /*******************************************
83
     * ONE QUERY
84
     *******************************************/
85
86
    /**
87
     * @param QueryInterface $query
88
     * @return mixed|null
89
     */
90
    public function findByQuery(QueryInterface $query)
91
    {
92
        return $this->queryOne($query);
93
    }
94
95
    /**
96
     * @param QueryInterface $query
97
     *
98
     * @return mixed
99
     */
100
    public function getByQuery(QueryInterface $query)
101
    {
102
        if (null === ($object = $this->findByQuery($query))) {
103
            $this->notFoundException();
104
        }
105
106
        return $object;
107
    }
108
109
    /*******************************************
110
     * ALL BY QUERY
111
     *******************************************/
112
113
    /**
114
     * @param QueryInterface $query
115
     * @return array
116
     */
117
    public function findAllByQuery(QueryInterface $query): array
118
    {
119
        return $this->queryAll($query);
120
    }
121
122
    /**
123
     * @param QueryInterface $query
124
     * @return array
125
     * @throws NotFoundException
126
     */
127
    public function getAllByQuery(QueryInterface $query): array
128
    {
129
        $records = $this->findAllByQuery($query);
130
        if (empty($records)) {
131
            $this->notFoundException();
132
        }
133
134
        return $records;
135
    }
136
137
138
    /*******************************************
139
     * CACHE
140
     *******************************************/
141
142
    /**
143
     * @param QueryInterface $query
144
     * @return mixed|null
145
     */
146
    protected function queryOne(QueryInterface $query)
147
    {
148
        $db = static::getDb();
149
150
        try {
151
            if (false === $this->cacheDuration) {
152
                return $query->one($db);
153
            }
154
155
            $result = $db->cache(function ($db) use ($query) {
156
                return $query->one($db);
157
            }, $this->cacheDuration, $this->cacheDependency);
158
        } catch (\Exception $e) {
159
            return null;
160
        }
161
162
        return $result;
163
    }
164
165
    /**
166
     *
167
     * @param QueryInterface $query
168
     * @return mixed[]
169
     */
170
    protected function queryAll(QueryInterface $query)
171
    {
172
        $db = static::getDb();
173
174
        try {
175
            if (false === $this->cacheDuration) {
176
                return $query->all($db);
177
            }
178
179
            $results = $db->cache(function ($db) use ($query) {
180
                return $query->all($db);
181
            }, $this->cacheDuration, $this->cacheDependency);
182
        } catch (\Exception $e) {
183
            return [];
184
        }
185
186
        return $results;
187
    }
188
189
    /*******************************************
190
     * EXCEPTIONS
191
     *******************************************/
192
193
    /**
194
     * @throws NotFoundException
195
     */
196
    protected function notFoundException()
197
    {
198
        throw new NotFoundException(
199
            sprintf(
200
                "Results not found."
201
            )
202
        );
203
    }
204
}
205