Passed
Push — master ( 26152a...8fe73e )
by F
04:04
created

LocalisationRegistrar   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 331
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 68
dl 0
loc 331
rs 10
c 0
b 0
f 0
wmc 24

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getAddressTypeClass() 0 3 1
A getLanguageClass() 0 3 1
A initializeCache() 0 8 1
A setLanguageClass() 0 5 1
A getLanguages() 0 21 3
A forgetCachedLanguages() 0 5 1
A getAddressClass() 0 3 1
A getCacheStoreFromConfig() 0 16 3
A getCacheStore() 0 3 1
A setAddressClass() 0 5 1
A __construct() 0 10 1
A getCurrencyClass() 0 3 1
A getAddresses() 0 22 4
A forgetCachedAddresses() 0 5 1
A registerLanguages() 0 3 1
A clearClassLanguages() 0 3 1
A getCountryClass() 0 3 1
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\Address\Type as AddressType;
20
use PWWeb\Localisation\Contracts\Country;
21
use PWWeb\Localisation\Contracts\Currency;
22
use PWWeb\Localisation\Contracts\Language;
23
24
class LocalisationRegistrar
25
{
26
    /**
27
     * The cache repository.
28
     *
29
     * @var Repository
30
     */
31
    protected $cache;
32
33
    /**
34
     * The cache manager object.
35
     *
36
     * @var \Illuminate\Cache\CacheManager
37
     */
38
    protected $cacheManager;
39
40
    /**
41
     * The address class used for the package.
42
     * Can be either original value or overwritten for custom use.
43
     *
44
     * @var string
45
     */
46
    protected $addressClass;
47
48
    /**
49
     * The country class used for the package.
50
     * Can be either original value or overwritten for custom use.
51
     *
52
     * @var string
53
     */
54
    protected $countryClass;
55
56
    /**
57
     * The currency class used for the package.
58
     * Can be either original value or overwritten for custom use.
59
     *
60
     * @var string
61
     */
62
    protected $currencyClass;
63
64
    /**
65
     * The language class used for the package.
66
     * Can be either original value or overwritten for custom use.
67
     *
68
     * @var string
69
     */
70
    protected $languageClass;
71
72
    /**
73
     * The set of addresses available in the cache.
74
     *
75
     * @var \Illuminate\Support\Collection
76
     */
77
    protected $addresses;
78
79
    /**
80
     * The set of languages available in the cache.
81
     *
82
     * @var \Illuminate\Support\Collection
83
     */
84
    protected $languages;
85
86
    /**
87
     * The cache expiration time.
88
     *
89
     * @var DateInterval|integer
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...
90
     */
91
    public static $cacheExpirationTime;
92
93
    /**
94
     * The cache key.
95
     *
96
     * @var string
97
     */
98
    public static $cacheKey;
99
100
    /**
101
     * The cache model key.
102
     *
103
     * @var string
104
     */
105
    public static $cacheModelKey;
106
107
    /**
108
     * PermissionRegistrar constructor.
109
     *
110
     * @param \Illuminate\Cache\CacheManager $cacheManager The cache manager object
111
     */
112
    public function __construct(CacheManager $cacheManager)
113
    {
114
        $this->addressClass = config('pwweb.localisation.models.address');
115
        $this->addressTypeClass = config('pwweb.localisation.models.address_type');
1 ignored issue
show
Bug Best Practice introduced by
The property addressTypeClass does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
116
        $this->countryClass = config('pwweb.localisation.models.country');
117
        $this->currencyClass = config('pwweb.localisation.models.currency');
118
        $this->languageClass = config('pwweb.localisation.models.language');
119
120
        $this->cacheManager = $cacheManager;
121
        $this->initializeCache();
122
    }
123
124
    /**
125
     * Initialize the cache for the package.
126
     *
127
     * @return void
128
     */
129
    protected function initializeCache()
130
    {
131
        self::$cacheExpirationTime = config('pwweb.localisation.cache.expiration_time', config('pwweb.localisation.cache_expiration_time'));
132
133
        self::$cacheKey = config('pwweb.localisation.cache.key');
134
        self::$cacheModelKey = config('pwweb.localisation.cache.model_key');
135
136
        $this->cache = $this->getCacheStoreFromConfig();
137
    }
138
139
    /**
140
     * Retrieve the cache store from the configuration of the package.
141
     *
142
     * @return Repository Cache store
143
     */
144
    protected function getCacheStoreFromConfig(): Repository
145
    {
146
        // the 'default' fallback here is from the localisation.php config file, where 'default' means to use config(cache.default)
147
        $cacheDriver = config('pwweb.localisation.cache.store', 'default');
148
149
        // when 'default' is specified, no action is required since we already have the default instance
150
        if ('default' === $cacheDriver) {
151
            return $this->cacheManager->store();
152
        }
153
154
        // if an undefined cache store is specified, fallback to 'array' which is Laravel's closest equiv to 'none'
155
        if (false === \array_key_exists($cacheDriver, config('cache.stores'))) {
156
            $cacheDriver = 'array';
157
        }
158
159
        return $this->cacheManager->store($cacheDriver);
160
    }
161
162
    /**
163
     * Register the languages check method.
164
     *
165
     * @return bool
166
     */
167
    public function registerLanguages(): bool
168
    {
169
        return true;
170
    }
171
172
    /**
173
     * Flush the cache.
174
     *
175
     * @return bool
176
     */
177
    public function forgetCachedAddresses()
178
    {
179
        $this->addresses = null;
180
181
        return $this->cache->forget(self::$cacheKey);
182
    }
183
184
    /**
185
     * Flush the cache.
186
     *
187
     * @return bool
188
     */
189
    public function forgetCachedLanguages()
190
    {
191
        $this->languages = null;
192
193
        return $this->cache->forget(self::$cacheKey);
194
    }
195
196
    /**
197
     * Clear class languages.
198
     * This is only intended to be called by the LocalisationServiceProvider on boot,
199
     * so that long-running instances like Swoole don't keep old data in memory.
200
     *
201
     * @return void
202
     */
203
    public function clearClassLanguages()
204
    {
205
        $this->languages = null;
206
    }
207
208
    /**
209
     * Get the addresses based on the passed params.
210
     *
211
     * @param array $params additional parameters for query
212
     *
213
     * @return \Illuminate\Support\Collection
214
     */
215
    public function getAddresses(array $params = []): Collection
216
    {
217
        if (null === $this->addresses) {
218
            $this->addresses = $this->cache->remember(
219
                self::$cacheKey . '.addresses',
220
                self::$cacheExpirationTime,
221
                function () {
222
                    return $this->getAddressClass()
223
                        //->with('system_address_types')
224
                        ->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

224
                        ->/** @scrutinizer ignore-call */ get();
Loading history...
225
                }
226
            );
227
        }
228
229
        $addresses = clone $this->addresses;
230
231
        foreach ($params as $attr => $value) {
232
            $attr = 'type' === $attr ? 'type.name' : $attr;
233
            $addresses = $addresses->where($attr, $value);
234
        }
235
236
        return $addresses;
237
    }
238
239
    /**
240
     * Get the languages based on the passed params.
241
     *
242
     * @param array $params additional parameters for query
243
     *
244
     * @return \Illuminate\Support\Collection
245
     */
246
    public function getLanguages(array $params = []): Collection
247
    {
248
        if (null === $this->languages) {
249
            $this->languages = $this->cache->remember(
250
                self::$cacheKey,
251
                self::$cacheExpirationTime,
252
                function () {
253
                    return $this->getLanguageClass()
254
                    //->with('countries')
255
                        ->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

255
                        ->/** @scrutinizer ignore-call */ get();
Loading history...
256
                }
257
            );
258
        }
259
260
        $languages = clone $this->languages;
261
262
        foreach ($params as $attr => $value) {
263
            $languages = $languages->where($attr, $value);
264
        }
265
266
        return $languages;
267
    }
268
269
    /**
270
     * Get an instance of the address class.
271
     *
272
     * @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...
273
     */
274
    public function getAddressClass(): Address
275
    {
276
        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...
277
    }
278
279
    /**
280
     * Get an instance of the address class.
281
     *
282
     * @return PWWeb\Localisation\Contract\Address\Type
0 ignored issues
show
Bug introduced by
The type PWWEB\Localisation\PWWeb...n\Contract\Address\Type 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...
283
     */
284
    public function getAddressTypeClass(): AddressType
285
    {
286
        return app($this->addressTypeClass);
0 ignored issues
show
Bug Best Practice introduced by
The expression return app($this->addressTypeClass) could return the type Illuminate\Contracts\Foundation\Application which is incompatible with the type-hinted return PWWeb\Localisation\Contracts\Address\Type. Consider adding an additional type-check to rule them out.
Loading history...
287
    }
288
289
    /**
290
     * Get an instance of the country class.
291
     *
292
     * @return PWWeb\Localisation\Contract\Country
0 ignored issues
show
Bug introduced by
The type PWWEB\Localisation\PWWeb...sation\Contract\Country 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...
293
     */
294
    public function getCountryClass(): Country
295
    {
296
        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...
297
    }
298
299
    /**
300
     * Get an instance of the currency class.
301
     *
302
     * @return PWWeb\Localisation\Contract\Currency
0 ignored issues
show
Bug introduced by
The type PWWEB\Localisation\PWWeb...ation\Contract\Currency 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...
303
     */
304
    public function getCurrencyClass(): Currency
305
    {
306
        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...
307
    }
308
309
    /**
310
     * Get an instance of the language class.
311
     *
312
     * @return PWWeb\Localisation\Contract\Language
0 ignored issues
show
Bug introduced by
The type PWWEB\Localisation\PWWeb...ation\Contract\Language 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...
313
     */
314
    public function getLanguageClass(): Language
315
    {
316
        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...
317
    }
318
319
    /**
320
     * Set the instance of the address class.
321
     *
322
     * @param string $addressClass the address class to be used
323
     *
324
     * @return object
325
     */
326
    public function setAddressClass(string $addressClass)
327
    {
328
        $this->addressClass = $addressClass;
329
330
        return $this;
331
    }
332
333
    /**
334
     * Set the instance of the language class.
335
     *
336
     * @param string $languageClass the language class to be used
337
     *
338
     * @return object
339
     */
340
    public function setLanguageClass(string $languageClass)
341
    {
342
        $this->languageClass = $languageClass;
343
344
        return $this;
345
    }
346
347
    /**
348
     * Get the instance of the Cache Store.
349
     *
350
     * @return \Illuminate\Contracts\Cache\Store
351
     */
352
    public function getCacheStore(): \Illuminate\Contracts\Cache\Store
353
    {
354
        return $this->cache->getStore();
355
    }
356
}
357