Passed
Push — master ( e00e24...e046f3 )
by Nate
06:00 queued 02:00
created

ObjectByHandle::find()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 4

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
nc 2
cc 2
eloc 4
nop 2
1
<?php
2
3
/**
4
 * @package    Spark
5
 * @author     Flipbox Factory <[email protected]>
6
 * @copyright  2010-2016 Flipbox Digital Limited
7
 * @license    https://github.com/FlipboxFactory/Craft3-Spark/blob/master/LICENSE
8
 * @link       https://github.com/FlipboxFactory/Craft3-Spark
9
 * @since      Class available since Release 1.0.0
10
 */
11
12
namespace flipbox\spark\services;
13
14
use flipbox\spark\exceptions\ObjectNotFoundException;
15
use flipbox\spark\objects\Object as BaseObject;
16
use flipbox\spark\objects\ObjectWithHandle;
17
use flipbox\spark\records\Record;
18
use flipbox\spark\records\RecordWithHandle;
19
20
abstract class ObjectByHandle extends Object
21
{
22
23
    /**
24
     * @var ObjectWithHandle[]
25
     */
26
    protected $_cacheByHandle = [];
27
28
    /**
29
     * @param $identifier
30
     * @param string $toScenario
31
     * @return BaseObject|ObjectWithHandle|null
32
     */
33 View Code Duplication
    public function find($identifier, string $toScenario = null)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
36
        if (is_string($identifier)) {
37
38
            return $this->findByHandle($identifier, $toScenario);
39
40
        }
41
42
        return parent::find($identifier, $toScenario);
43
44
    }
45
46
    /*******************************************
47
     * FIND/GET BY HANDLE
48
     *******************************************/
49
50
    /**
51
     * @param string $handle
52
     * @param string|null $toScenario
53
     * @return ObjectWithHandle|null
54
     */
55 View Code Duplication
    public function findByHandle(string $handle, string $toScenario = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
58
        // Check cache
59
        if (!$object = $this->findCacheByHandle($handle)) {
60
61
            // Find record in db
62
            if ($record = $this->findRecordByHandle($handle)) {
63
64
                /** @var ObjectWithHandle $object */
65
                $object = $this->findByRecord($record, $toScenario);
66
67
            } else {
68
69
                $this->_cacheByHandle[$handle] = null;
70
71
                return null;
72
73
            }
74
75
        }
76
77
        return $object;
78
79
    }
80
81
    /**
82
     * @param string $handle
83
     * @param string|null $toScenario
84
     * @return ObjectWithHandle|null
85
     * @throws ObjectNotFoundException
86
     */
87 View Code Duplication
    public function getByHandle(string $handle, string $toScenario = null): ObjectWithHandle
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
90
        if (!$object = $this->findByHandle($handle, $toScenario)) {
91
92
            $this->notFoundByHandleException($handle);
93
94
        }
95
96
        return $object;
97
98
    }
99
100
    /**
101
     * @param string $handle
102
     * @param string|null $toScenario
103
     * @return ObjectWithHandle|null
104
     */
105
    public function freshFindByHandle(string $handle, string $toScenario = null)
106
    {
107
108
        // Find record in db
109
        if (!$record = $this->findRecordByHandle($handle)) {
110
            return null;
111
        }
112
113
        /** @var ObjectWithHandle $object */
114
        $object = $this->createFromRecord($record, $toScenario);
115
116
        return $object;
117
118
    }
119
120
    /**
121
     * @param string $handle
122
     * @param string|null $toScenario
123
     * @return ObjectWithHandle
124
     * @throws ObjectNotFoundException
125
     */
126 View Code Duplication
    public function freshGetByHandle(string $handle, string $toScenario = null): ObjectWithHandle
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
    {
128
129
        if (!$object = $this->freshFindByHandle($handle, $toScenario)) {
130
131
            $this->notFoundByHandleException($handle);
132
133
        }
134
135
        return $object;
136
137
    }
138
139
140
    /*******************************************
141
     * CACHE
142
     *******************************************/
143
144
    /**
145
     * @inheritdoc
146
     * @return BaseObject|ObjectWithHandle|null
147
     */
148
    public function findCache($identifier)
149
    {
150
151
        if (is_string($identifier)) {
152
153
            return $this->findCacheByHandle($identifier);
154
155
        }
156
157
        return parent::findCache($identifier);
158
159
    }
160
161
    /**
162
     * @inheritdoc
163
     */
164
    public function addToCache(BaseObject $object)
165
    {
166
167
        if ($object instanceof ObjectWithHandle) {
168
169
            $this->cacheByHandle($object);
170
171
        }
172
173
        return parent::addToCache($object);
174
175
    }
176
177
    /**
178
     * Find an existing cache by handle
179
     *
180
     * @param string $handle
181
     * @return null
182
     */
183
    public function findCacheByHandle(string $handle)
184
    {
185
186
        // Check if already in addToCache
187
        if (!$this->isCachedByHandle($handle)) {
188
            return null;
189
        }
190
191
        return $this->_cacheByHandle[$handle];
192
193
    }
194
195
    /**
196
     * Identify whether in cache by handle
197
     *
198
     * @param string $handle
199
     * @return bool
200
     */
201
    private function isCachedByHandle(string $handle): bool
202
    {
203
        return array_key_exists($handle, $this->_cacheByHandle);
204
    }
205
206
    /**
207
     * @param ObjectWithHandle $object
208
     * @return static
209
     */
210
    protected function cacheByHandle(ObjectWithHandle $object)
211
    {
212
213
        // Check if already in cache
214
        if (!$this->isCachedByHandle($object->handle)) {
215
216
            // Cache it
217
            $this->_cacheByHandle[$object->handle] = $object;
218
219
        }
220
221
        return $this;
222
223
    }
224
225
    /**
226
     * @param Record $record
227
     * @return BaseObject|ObjectWithHandle|null
228
     */
229 View Code Duplication
    public function findCacheByRecord(Record $record)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
230
    {
231
232
        if ($record instanceof RecordWithHandle) {
233
234
            // Check if already in cache by id
235
            if (!$this->isCachedByHandle($record->handle)) {
236
237
                return $this->findCacheByHandle($record->handle);
238
239
            }
240
241
        }
242
243
        return parent::findCacheByRecord($record);
244
245
    }
246
247
    /*******************************************
248
     * RECORD BY HANDLE
249
     *******************************************/
250
251
    /**
252
     * @param string $handle
253
     * @param string|null $toScenario
254
     * @return RecordWithHandle|null
255
     */
256
    protected function findRecordByHandle(string $handle, string $toScenario = null)
257
    {
258
259
        return $this->findRecordByCondition(
260
            [
261
                'handle' => $handle
262
            ],
263
            $toScenario
264
        );
265
266
    }
267
268
    /*******************************************
269
     * EXCEPTIONS
270
     *******************************************/
271
272
    /**
273
     * @param string|null $handle
274
     * @throws ObjectNotFoundException
275
     */
276
    protected function notFoundByHandleException(string $handle = null)
277
    {
278
279
        throw new ObjectNotFoundException(
280
            sprintf(
281
                'Object does not exist with the handle "%s".',
282
                (string)$handle
283
            )
284
        );
285
286
    }
287
288
}
289