Passed
Push — master ( f2dea1...58a092 )
by Nate
04:47
created

ElementByString   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 208
Duplicated Lines 6.25 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 13
loc 208
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
stringProperty() 0 1 ?
freshFindByString() 0 1 ?
A stringValue() 0 8 1
B findByString() 0 24 3
A getByString() 0 13 2
A freshGetByString() 0 12 2
A findCacheByString() 0 13 2
A isCachedByString() 13 13 2
B cacheByString() 0 25 4
A notFoundByStringException() 0 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/spark/blob/master/LICENSE
6
 * @link       https://github.com/flipbox/spark
7
 */
8
9
namespace flipbox\spark\services\traits;
10
11
use craft\base\Element as BaseElement;
12
use craft\base\ElementInterface;
13
use flipbox\spark\exceptions\ElementNotFoundException;
14
use flipbox\spark\helpers\SiteHelper;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.2.0
19
 */
20
trait ElementByString
21
{
22
23
    /**
24
     * @var [ElementInterface[]]
25
     */
26
    protected $_cacheByString = [];
27
28
    /*******************************************
29
     * ABSTRACTS
30
     *******************************************/
31
32
    /**
33
     * @return string
34
     */
35
    abstract protected function stringProperty(): string;
36
37
    /**
38
     * @param $string
39
     * @param int|null $siteId
40
     * @return BaseElement|ElementInterface|null
41
     */
42
    abstract protected function freshFindByString(string $string, int $siteId = null);
43
44
    /**
45
     * @param ElementInterface $element
46
     * @return string
47
     */
48
    protected function stringValue(ElementInterface $element): string
49
    {
50
51
        $property = $this->stringProperty();
52
53
        return $element->{$property};
54
55
    }
56
57
    /**
58
     * @param string $string
59
     * @param int|null $siteId
60
     * @return BaseElement|ElementInterface|null
61
     */
62
    public function findByString(string $string, int $siteId = null)
63
    {
64
65
        // Check cache
66
        if (!$element = $this->findCacheByString($string)) {
67
68
            // Find new element
69
            if ($element = $this->freshFindByString($string, $siteId)) {
70
71
                // Cache it
72
                $this->cacheByString($element);
73
74
            } else {
75
76
                // Cache nothing
77
                $this->_cacheByString[$string] = $element;
78
79
            }
80
81
        }
82
83
        return $element;
84
85
    }
86
87
    /**
88
     * @param string $string
89
     * @param int|null $siteId
90
     * @return BaseElement|ElementInterface
91
     * @throws ElementNotFoundException
92
     */
93
    public function getByString(string $string, int $siteId = null)
94
    {
95
96
        // Find by Handle
97
        if (!$element = $this->findByString($string, $siteId)) {
98
99
            $this->notFoundByStringException($string);
100
101
        }
102
103
        return $element;
104
105
    }
106
107
108
    /**
109
     * @param string $string
110
     * @param int|null $siteId
111
     * @return BaseElement|ElementInterface
112
     * @throws ElementNotFoundException
113
     */
114
    public function freshGetByString(string $string, int $siteId = null)
115
    {
116
117
        if (!$element = $this->freshFindByString($string, $siteId)) {
118
119
            $this->notFoundByStringException($string);
120
121
        }
122
123
        return $element;
124
125
    }
126
127
128
    /*******************************************
129
     * CACHE
130
     *******************************************/
131
132
    /**
133
     * Find an existing cache by Handle
134
     *
135
     * @param string $string
136
     * @param int|null $siteId
137
     * @return BaseElement|ElementInterface|null
138
     */
139
    public function findCacheByString(string $string, int $siteId = null)
140
    {
141
142
        // Check if already in cache
143
        if ($this->isCachedByString($string, $siteId)) {
144
145
            return $this->_cacheByString[$siteId][$string];
146
147
        }
148
149
        return null;
150
151
    }
152
153
    /**
154
     * Identify whether in cached by string
155
     *
156
     * @param $string
157
     * @param int|null $siteId
158
     * @return bool
159
     */
160 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...
161
    {
162
163
        // Resolve siteId
164
        $siteId = SiteHelper::resolveSiteId($siteId);
165
166
        if (!isset($this->_cacheByString[$siteId])) {
167
            $this->_cacheByString[$siteId] = [];
168
        }
169
170
        return array_key_exists($string, $this->_cacheByString[$siteId]);
171
172
    }
173
174
    /**
175
     * @param ElementInterface $element
176
     * @return $this
177
     */
178
    protected function cacheByString(ElementInterface $element)
179
    {
180
181
        /** @var BaseElement $element */
182
183
        $stringValue = $this->stringValue($element);
184
185
        if (null === $stringValue) {
186
            return $this;
187
        }
188
189
        // Resolve siteId
190
        $siteId = SiteHelper::resolveSiteId($element->siteId);
191
192
        // Check if already in cache
193
        if ($stringValue && !$this->isCachedByString($stringValue, $siteId)) {
194
195
            // Cache it
196
            $this->_cacheByString[$siteId][$stringValue] = $element;
197
198
        }
199
200
        return $this;
201
202
    }
203
204
205
206
207
    /*******************************************
208
     * EXCEPTIONS
209
     *******************************************/
210
211
    /**
212
     * @param null $string
213
     * @throws ElementNotFoundException
214
     */
215
    protected function notFoundByStringException($string = null)
216
    {
217
218
        throw new ElementNotFoundException(
219
            sprintf(
220
                'Element does not exist with the string "%s".',
221
                (string)$string
222
            )
223
        );
224
225
    }
226
227
}
228