Completed
Push — master ( 193c3c...281dde )
by Nate
07:20
created

ElementAccessorByString   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 4
dl 0
loc 245
ccs 0
cts 106
cp 0
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
stringProperty() 0 1 ?
A stringValue() 0 6 1
A addToCache() 0 6 1
A findByString() 0 15 3
A getByString() 0 8 2
A freshFindByString() 0 15 2
A freshGetByString() 0 8 2
A findCacheByString() 0 10 2
A isCachedByString() 0 10 2
A cacheByString() 0 19 3
A notFoundByStringException() 0 9 1
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;
10
11
use craft\base\Element;
12
use craft\base\ElementInterface;
13
use craft\elements\db\ElementQuery;
14
use flipbox\ember\exceptions\ElementNotFoundException;
15
use flipbox\ember\helpers\SiteHelper;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
trait ElementAccessorByString
22
{
23
    use ElementAccessor {
24
        find as parentFind;
25
        findCache as parentFindCache;
26
    }
27
28
    /**
29
     * @var [ElementInterface[]]
30
     */
31
    protected $cacheByString = [];
32
33
    /**
34
     * @return string
35
     */
36
    abstract protected function stringProperty(): string;
37
38
    /*******************************************
39
     * STRING
40
     *******************************************/
41
42
    /**
43
     * @param ElementInterface $element
44
     * @return string
45
     */
46
    protected function stringValue(ElementInterface $element)
47
    {
48
        $property = $this->stringProperty();
49
50
        return $element->{$property};
51
    }
52
53
54
    /*******************************************
55
     * FIND OVERRIDES
56
     *******************************************/
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function find($identifier, int $siteId = null, string $toScenario = null)
62
    {
63
        if ($element = $this->parentFind($identifier, $siteId, $toScenario)) {
0 ignored issues
show
Bug introduced by
It seems like parentFind() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
64
            return $element;
65
        }
66
67
        if (!is_string($identifier)) {
68
            return null;
69
        }
70
71
        return $this->findByString($identifier, $siteId, $toScenario);
72
    }
73
74
    /*******************************************
75
     * CACHE OVERRIDES
76
     *******************************************/
77
78
    /**
79
     * @inheritdoc
80
     */
81
    public function findCache($identifier, int $siteId = null)
82
    {
83
        if ($element = $this->parentFindCache($identifier, $siteId)) {
0 ignored issues
show
Bug introduced by
The method parentFindCache() does not exist on flipbox\ember\services\t...ElementAccessorByString. Did you maybe mean findCache()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
84
            return $element;
85
        }
86
87
        if (!is_string($identifier)) {
88
            return null;
89
        }
90
91
        return $this->findCacheByString($identifier, $siteId);
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97
    public function addToCache(ElementInterface $element)
98
    {
99
        $this->cacheById($element);
100
        $this->cacheByString($element);
101
        return $this;
102
    }
103
104
105
    /*******************************************
106
     * FIND/GET BY STRING
107
     *******************************************/
108
109
    /**
110
     * @param string $string
111
     * @param int|null $siteId
112
     * @param string|null $toScenario
113
     * @return ElementInterface|null
114
     */
115
    public function findByString(string $string, int $siteId = null, string $toScenario = null)
116
    {
117
        $siteId = SiteHelper::resolveSiteId($siteId);
118
119
        if (!$element = $this->findCacheByString($string, $siteId)) {
120
            if (!$element = $this->freshFindByString($string, $siteId)) {
121
                $this->cacheByString[$siteId][$string] = null;
122
                return null;
123
            }
124
125
            $this->addToCache($element);
126
        }
127
128
        return $this->applyScenario($element, $toScenario);
129
    }
130
131
    /**
132
     * @param string $string
133
     * @param string|null $toScenario
134
     * @return ElementInterface|null
135
     * @throws ElementNotFoundException
136
     */
137
    public function getByString(string $string, string $toScenario = null): ElementInterface
138
    {
139
        if (!$element = $this->findByString($string, $toScenario)) {
140
            $this->notFoundByStringException($string);
141
        }
142
143
        return $element;
144
    }
145
146
    /**
147
     * @param string $string
148
     * @param int|null $siteId
149
     * @param string $toScenario
150
     * @return ElementInterface|null
151
     */
152
    public function freshFindByString(string $string, int $siteId = null, string $toScenario = null)
153
    {
154
        /** @var ElementQuery $query */
155
        $query = $this->getQuery();
156
        $query->{$this->stringProperty()} = $string;
157
        $query->siteId = $siteId;
158
        $query->status = null;
159
        $query->enabledForSite = false;
160
161
        if (!$element = $query->one()) {
162
            return null;
163
        }
164
165
        return $this->applyScenario($element, $toScenario);
166
    }
167
168
    /**
169
     * @param string $string
170
     * @param string|null $toScenario
171
     * @return ElementInterface
172
     * @throws ElementNotFoundException
173
     */
174
    public function freshGetByString(string $string, string $toScenario = null): ElementInterface
175
    {
176
        if (!$element = $this->freshFindByString($string, $toScenario)) {
177
            $this->notFoundByStringException($string);
178
        }
179
180
        return $element;
181
    }
182
183
184
    /*******************************************
185
     * CACHE BY STRING
186
     *******************************************/
187
188
    /**
189
     * Find an existing cache by ID
190
     *
191
     * @param string $string
192
     * @param int|null $siteId
193
     * @return ElementInterface|null
194
     */
195
    public function findCacheByString(string $string, int $siteId = null)
196
    {
197
        $siteId = SiteHelper::resolveSiteId($siteId);
198
199
        if ($this->isCachedByString($string, $siteId)) {
200
            return $this->cacheByString[$siteId][$string];
201
        }
202
203
        return null;
204
    }
205
206
    /**
207
     * Identify whether in cached by ID
208
     *
209
     * @param string $string
210
     * @param int|null $siteId
211
     * @return bool
212
     */
213
    protected function isCachedByString(string $string, int $siteId = null): bool
214
    {
215
        $siteId = SiteHelper::resolveSiteId($siteId);
216
217
        if (!array_key_exists($siteId, $this->cacheByString)) {
218
            $this->cacheByString[$siteId] = [];
219
        }
220
221
        return array_key_exists($string, $this->cacheByString[$siteId]);
222
    }
223
224
    /**
225
     * @param ElementInterface|Element $element
226
     * @return $this
227
     */
228
    protected function cacheByString(ElementInterface $element)
229
    {
230
        $stringValue = $this->stringValue($element);
231
232
        if (null === $stringValue) {
233
            return $this;
234
        }
235
236
        $siteId = $element->siteId;
1 ignored issue
show
Bug introduced by
Accessing siteId on the interface craft\base\ElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
237
238
        // todo - ensure siteId is set?
239
240
        // Check if already in cache
241
        if (!$this->isCachedByString($stringValue, $siteId)) {
242
            $this->cacheByString[$siteId][$stringValue] = $element;
243
        }
244
245
        return $this;
246
    }
247
248
    /*******************************************
249
     * EXCEPTIONS
250
     *******************************************/
251
252
    /**
253
     * @param string|null $string
254
     * @throws ElementNotFoundException
255
     */
256
    protected function notFoundByStringException(string $string = null)
257
    {
258
        throw new ElementNotFoundException(
259
            sprintf(
260
                'Element does not exist with the string "%s".',
261
                (string)$string
262
            )
263
        );
264
    }
265
}
266