Passed
Push — master ( 6bcc6d...405e10 )
by F
03:06
created

LocalisationRegistrar::setAddressClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
/**
4
 * PWWeb\Localisation.
5
 *
6
 * Localisation Registrar.
7
 *
8
 * @author    Frank Pillukeit <[email protected]>
9
 * @copyright 2020 pw-websolutions.com
10
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
11
 */
12
13
namespace PWWeb\Localisation;
14
15
use Illuminate\Cache\CacheManager;
16
use Illuminate\Contracts\Cache\Repository;
17
use Illuminate\Support\Collection;
18
use PWWeb\Localisation\Contracts\Address;
19
use PWWeb\Localisation\Contracts\Country;
20
use PWWeb\Localisation\Contracts\Currency;
21
use PWWeb\Localisation\Contracts\Language;
22
23
class LocalisationRegistrar
24
{
25
    /**
26
     * The cache repository.
27
     *
28
     * @var Repository
29
     */
30
    protected $cache;
31
32
    /**
33
     * The cache manager object.
34
     *
35
     * @var \Illuminate\Cache\CacheManager
36
     */
37
    protected $cacheManager;
38
39
    /**
40
     * The address class used for the package.
41
     * Can be either original value or overwritten for custom use.
42
     *
43
     * @var string
44
     */
45
    protected $addressClass;
46
47
    /**
48
     * The country class used for the package.
49
     * Can be either original value or overwritten for custom use.
50
     *
51
     * @var string
52
     */
53
    protected $countryClass;
54
55
    /**
56
     * The currency class used for the package.
57
     * Can be either original value or overwritten for custom use.
58
     *
59
     * @var string
60
     */
61
    protected $currencyClass;
62
63
    /**
64
     * The language class used for the package.
65
     * Can be either original value or overwritten for custom use.
66
     *
67
     * @var string
68
     */
69
    protected $languageClass;
70
71
    /**
72
     * The set of addresses available in the cache.
73
     *
74
     * @var \Illuminate\Support\Collection
75
     */
76
    protected $addresses;
77
78
    /**
79
     * The set of languages available in the cache.
80
     *
81
     * @var \Illuminate\Support\Collection
82
     */
83
    protected $languages;
84
85
    /**
86
     * The cache expiration time.
87
     *
88
     * @var DateInterval|int
0 ignored issues
show
Bug introduced by
The type PWWeb\Localisation\DateInterval was not found. Did you mean DateInterval? If so, make sure to prefix the type with \.
Loading history...
89
     */
90
    public static $cacheExpirationTime;
91
92
    /**
93
     * The cache key.
94
     *
95
     * @var string
96
     */
97
    public static $cacheKey;
98
99
    /**
100
     * The cache model key.
101
     *
102
     * @var string
103
     */
104
    public static $cacheModelKey;
105
106
    /**
107
     * PermissionRegistrar constructor.
108
     *
109
     * @param \Illuminate\Cache\CacheManager $cacheManager The cache manager object
110
     */
111
    public function __construct(CacheManager $cacheManager)
112
    {
113
        $this->addressClass = config('localisation.models.address');
114
        $this->countryClass = config('localisation.models.country');
115
        $this->currencyClass = config('localisation.models.currency');
116
        $this->languageClass = config('localisation.models.language');
117
118
        $this->cacheManager = $cacheManager;
119
        $this->initializeCache();
120
    }
121
122
    /**
123
     * Initialize the cache for the package.
124
     *
125
     * @return void
126
     */
127
    protected function initializeCache()
128
    {
129
        self::$cacheExpirationTime = config('localisation.cache.expiration_time', config('localisation.cache_expiration_time'));
130
131
        self::$cacheKey = config('localisation.cache.key');
132
        self::$cacheModelKey = config('localisation.cache.model_key');
133
134
        $this->cache = $this->getCacheStoreFromConfig();
135
    }
136
137
    /**
138
     * Retrieve the cache store from the configuration of the package.
139
     *
140
     * @return Repository Cache store
141
     */
142
    protected function getCacheStoreFromConfig(): Repository
143
    {
144
        // the 'default' fallback here is from the localisation.php config file, where 'default' means to use config(cache.default)
145
        $cacheDriver = config('localisation.cache.store', 'default');
146
147
        // when 'default' is specified, no action is required since we already have the default instance
148
        if ('default' === $cacheDriver) {
149
            return $this->cacheManager->store();
150
        }
151
152
        // if an undefined cache store is specified, fallback to 'array' which is Laravel's closest equiv to 'none'
153
        if (false === \array_key_exists($cacheDriver, config('cache.stores'))) {
154
            $cacheDriver = 'array';
155
        }
156
157
        return $this->cacheManager->store($cacheDriver);
158
    }
159
160
    /**
161
     * Register the languages check method.
162
     */
163
    public function registerLanguages(): bool
164
    {
165
        return true;
166
    }
167
168
    /**
169
     * Flush the cache.
170
     *
171
     * @return bool
172
     */
173
    public function forgetCachedAddresses()
174
    {
175
        $this->addresses = null;
176
177
        return $this->cache->forget(self::$cacheKey);
178
    }
179
180
    /**
181
     * Flush the cache.
182
     *
183
     * @return bool
184
     */
185
    public function forgetCachedLanguages()
186
    {
187
        $this->languages = null;
188
189
        return $this->cache->forget(self::$cacheKey);
190
    }
191
192
    /**
193
     * Clear class languages.
194
     * This is only intended to be called by the LocalisationServiceProvider on boot,
195
     * so that long-running instances like Swoole don't keep old data in memory.
196
     *
197
     * @return void
198
     */
199
    public function clearClassLanguages()
200
    {
201
        $this->languages = null;
202
    }
203
204
    /**
205
     * Get the addresses based on the passed params.
206
     *
207
     * @param array $params additional parameters for query
208
     */
209
    public function getAddresses(array $params = []): Collection
210
    {
211
        if (null === $this->addresses) {
212
            $this->addresses = $this->cache->remember(
213
                self::$cacheKey.'.addresses',
214
                self::$cacheExpirationTime,
215
                function () {
216
                    return $this->getAddressClass()
217
                    //->with('countries')
218
                        ->get();
0 ignored issues
show
Bug introduced by
The method get() does not exist on PWWeb\Localisation\Contracts\Address. Since it exists in all sub-types, consider adding an abstract or default implementation to PWWeb\Localisation\Contracts\Address. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

218
                        ->/** @scrutinizer ignore-call */ get();
Loading history...
219
                }
220
            );
221
        }
222
223
        $addresses = clone $this->addresses;
224
225
        foreach ($params as $attr => $value) {
226
            $addresses = $addresses->where($attr, $value);
227
        }
228
229
        return $addresses;
230
    }
231
232
    /**
233
     * Get the languages based on the passed params.
234
     *
235
     * @param array $params additional parameters for query
236
     */
237
    public function getLanguages(array $params = []): Collection
238
    {
239
        if (null === $this->languages) {
240
            $this->languages = $this->cache->remember(
241
                self::$cacheKey,
242
                self::$cacheExpirationTime,
243
                function () {
244
                    return $this->getLanguageClass()
245
                    //->with('countries')
246
                        ->get();
0 ignored issues
show
Bug introduced by
The method get() does not exist on PWWeb\Localisation\Contracts\Language. Since it exists in all sub-types, consider adding an abstract or default implementation to PWWeb\Localisation\Contracts\Language. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

246
                        ->/** @scrutinizer ignore-call */ get();
Loading history...
247
                }
248
            );
249
        }
250
251
        $languages = clone $this->languages;
252
253
        foreach ($params as $attr => $value) {
254
            $languages = $languages->where($attr, $value);
255
        }
256
257
        return $languages;
258
    }
259
260
    /**
261
     * Get an instance of the address class.
262
     *
263
     * @return PWWeb\Localisation\Contract\Address
0 ignored issues
show
Bug introduced by
The type PWWeb\Localisation\PWWeb...sation\Contract\Address was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
264
     */
265
    public function getAddressClass(): Address
266
    {
267
        return app($this->addressClass);
0 ignored issues
show
Bug Best Practice introduced by
The expression return app($this->addressClass) could return the type Illuminate\Contracts\Foundation\Application which is incompatible with the type-hinted return PWWeb\Localisation\Contracts\Address. Consider adding an additional type-check to rule them out.
Loading history...
268
    }
269
270
    /**
271
     * Get an instance of the country class.
272
     */
273
    public function getCountryClass(): Country
274
    {
275
        return app($this->countryClass);
0 ignored issues
show
Bug Best Practice introduced by
The expression return app($this->countryClass) could return the type Illuminate\Contracts\Foundation\Application which is incompatible with the type-hinted return PWWeb\Localisation\Contracts\Country. Consider adding an additional type-check to rule them out.
Loading history...
276
    }
277
278
    /**
279
     * Get an instance of the currency class.
280
     */
281
    public function getCurrencyClass(): Currency
282
    {
283
        return app($this->currencyClass);
0 ignored issues
show
Bug Best Practice introduced by
The expression return app($this->currencyClass) could return the type Illuminate\Contracts\Foundation\Application which is incompatible with the type-hinted return PWWeb\Localisation\Contracts\Currency. Consider adding an additional type-check to rule them out.
Loading history...
284
    }
285
286
    /**
287
     * Get an instance of the language class.
288
     */
289
    public function getLanguageClass(): Language
290
    {
291
        return app($this->languageClass);
0 ignored issues
show
Bug Best Practice introduced by
The expression return app($this->languageClass) could return the type Illuminate\Contracts\Foundation\Application which is incompatible with the type-hinted return PWWeb\Localisation\Contracts\Language. Consider adding an additional type-check to rule them out.
Loading history...
292
    }
293
294
    /**
295
     * Set the instance of the address class.
296
     *
297
     * @param string $addressClass the address class to be used
298
     *
299
     * @return object
300
     */
301
    public function setAddressClass(string $addressClass)
302
    {
303
        $this->addressClass = $addressClass;
304
305
        return $this;
306
    }
307
308
    /**
309
     * Set the instance of the language class.
310
     *
311
     * @param string $languageClass the language class to be used
312
     *
313
     * @return object
314
     */
315
    public function setLanguageClass(string $languageClass)
316
    {
317
        $this->languageClass = $languageClass;
318
319
        return $this;
320
    }
321
322
    /**
323
     * Get the instance of the Cache Store.
324
     */
325
    public function getCacheStore(): \Illuminate\Contracts\Cache\Store
326
    {
327
        return $this->cache->getStore();
328
    }
329
}
330