Passed
Push — master ( a018a6...5a9c7e )
by Nate
02:50
created

ElementByString::cacheByString()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
nc 2
cc 3
eloc 6
nop 1
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.2.0
10
 */
11
12
namespace flipbox\spark\services;
13
14
use craft\base\Element as BaseElement;
15
use craft\base\ElementInterface;
16
use flipbox\spark\exceptions\ElementNotFoundException;
17
use flipbox\spark\helpers\SiteHelper;
18
19
abstract class ElementByString extends Element
20
{
21
22
    /**
23
     * @var [ElementInterface[]]
24
     */
25
    protected $_cacheByString = [];
26
27
    /*******************************************
28
     * ABSTRACTS
29
     *******************************************/
30
31
    /**
32
     * @param $string
33
     * @param int|null $siteId
34
     * @return BaseElement|ElementInterface|null
35
     */
36
    abstract protected function freshFindByString(string $string, int $siteId = null);
37
38
    /**
39
     * @param ElementInterface $element
40
     * @return string
41
     */
42
    abstract protected function stringValue(ElementInterface $element);
43
44
45
    /**
46
     * @param $string
47
     * @param int|null $siteId
48
     * @return BaseElement|ElementInterface
49
     * @throws ElementNotFoundException
50
     */
51
    public function freshGetByString($string, int $siteId = null)
52
    {
53
54
        if (!$element = $this->freshFindByString($string, $siteId)) {
55
56
            $this->notFoundByStringException($string);
57
58
        }
59
60
        return $element;
61
62
    }
63
64
65
    /*******************************************
66
     * FIND
67
     *******************************************/
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function find($identifier, int $siteId = null)
73
    {
74
75
        if (is_string($identifier)) {
76
77
            return $this->findByString($identifier, $siteId);
78
79
        }
80
81
        return parent::find($identifier, $siteId);
82
83
    }
84
85
    /**
86
     * @param $string
87
     * @param int|null $siteId
88
     * @return BaseElement|ElementInterface|null
89
     */
90
    public function findByString($string, int $siteId = null)
91
    {
92
93
        // Check cache
94
        if (!$element = $this->findCacheByString($string)) {
95
96
            // Find new element
97
            if ($element = $this->freshFindByString($string, $siteId)) {
98
99
                // Cache it
100
                $this->cacheByString($element);
101
102
            } else {
103
104
                // Cache nothing
105
                $this->_cacheByString[$string] = $element;
106
107
            }
108
109
        }
110
111
        return $element;
112
113
    }
114
115
116
    /*******************************************
117
     * GET
118
     *******************************************/
119
120
    /**
121
     * @param $string
122
     * @param int|null $siteId
123
     * @return BaseElement|ElementInterface
124
     * @throws ElementNotFoundException
125
     */
126
    public function getByString($string, int $siteId = null)
127
    {
128
129
        // Find by Handle
130
        if (!$element = $this->findByString($string, $siteId)) {
131
132
            $this->notFoundByStringException($string);
133
134
        }
135
136
        return $element;
137
138
    }
139
140
141
    /*******************************************
142
     * CACHE
143
     *******************************************/
144
145
    /**
146
     * Find an existing cache by Handle
147
     *
148
     * @param $string
149
     * @param int|null $siteId
150
     * @return BaseElement|ElementInterface|null
151
     */
152
    public function findCacheByString($string, int $siteId = null)
153
    {
154
155
        // Check if already in cache
156
        if ($this->isCachedByString($string, $siteId)) {
157
158
            return $this->_cacheByString[$siteId][$string];
159
160
        }
161
162
        return null;
163
164
    }
165
166
    /**
167
     * Identify whether in cached by string
168
     *
169
     * @param $string
170
     * @param int|null $siteId
171
     * @return bool
172
     */
173 View Code Duplication
    protected function isCachedByString($string, int $siteId = 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...
174
    {
175
176
        // Resolve siteId
177
        $siteId = SiteHelper::resolveSiteId($siteId);
178
179
        if (!isset($this->_cacheByString[$siteId])) {
180
            $this->_cacheByString[$siteId] = [];
181
        }
182
183
        return array_key_exists($string, $this->_cacheByString);
184
185
    }
186
187
    /**
188
     * @param ElementInterface $element
189
     * @return $this
190
     */
191
    protected function cacheByString(ElementInterface $element)
192
    {
193
194
        /** @var BaseElement $element */
195
196
        $stringValue = $this->stringValue($element);
197
198
        // Resolve siteId
199
        $siteId = SiteHelper::resolveSiteId($element->siteId);
200
201
        // Check if already in cache
202
        if ($stringValue && !$this->isCachedByString($stringValue, $siteId)) {
203
204
            // Cache it
205
            $this->_cacheByString[$siteId][$stringValue] = $element;
206
207
        }
208
209
        return $this;
210
211
    }
212
213
    /**
214
     * @inheritdoc
215
     */
216
    public function findCache($identifier, int $siteId = null)
217
    {
218
219
        if (is_string($identifier)) {
220
221
            return $this->findCacheByString($identifier, $siteId);
222
223
        }
224
225
        return parent::findCache($identifier, $siteId);
226
227
    }
228
229
    /**
230
     * @inheritdoc
231
     */
232
    public function addToCache(ElementInterface $element)
233
    {
234
235
        // Cache by String
236
        $this->cacheByString($element);
237
238
        return parent::addToCache($element);
239
240
    }
241
242
    /*******************************************
243
     * EXCEPTIONS
244
     *******************************************/
245
246
    /**
247
     * @param null $string
248
     * @throws ElementNotFoundException
249
     */
250
    protected function notFoundByStringException($string = null)
251
    {
252
253
        throw new ElementNotFoundException(
254
            sprintf(
255
                'Element does not exist with the string "%s".',
256
                (string)$string
257
            )
258
        );
259
260
    }
261
262
}
263