Completed
Push — master ( dd8ca6...ed1ab1 )
by Antonio Carlos
06:33 queued 01:44
created

Repository   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 325
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 95.83%

Importance

Changes 0
Metric Value
wmc 22
lcom 2
cbo 5
dl 0
loc 325
ccs 69
cts 72
cp 0.9583
rs 10
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A __call() 0 4 1
A call() 0 14 3
A getHelper() 0 4 1
A getHydrator() 0 4 1
A boot() 0 6 1
A loadCountries() 0 12 1
A loadCountriesJson() 0 12 2
A loadCurrenciesForCountry() 0 8 1
A all() 0 4 1
A currencies() 0 12 1
A makeAllFlags() 0 20 1
A getFlagSvg() 0 6 1
A getFlagSvgPath() 0 4 1
A getGeometry() 0 6 1
A getTopology() 0 6 1
A hydrate() 0 4 1
A findTimezones() 0 6 1
A findTimezoneTime() 0 6 1
1
<?php
2
3
namespace PragmaRX\Countries\Package\Data;
4
5
use IlluminateAgnostic\Str\Support\Str;
6
use PragmaRX\Countries\Package\Services\Helper;
7
use PragmaRX\Countries\Package\Services\Hydrator;
8
use Psr\SimpleCache\CacheInterface as CacheContract;
9
use PragmaRX\Countries\Package\Services\Cache\Service as Cache;
10
11
class Repository
12
{
13
    /**
14
     * Timezones.
15
     *
16
     * @var
17
     */
18
    public $timezones;
19
20
    /**
21
     * Countries json.
22
     *
23
     * @var
24
     */
25
    public $countriesJson;
26
27
    /**
28
     * Countries.
29
     *
30
     * @var array
31
     */
32
    public $countries = [];
33
34
    /**
35
     * Hydrator.
36
     *
37
     * @var Hydrator
38
     */
39
    public $hydrator;
40
41
    /**
42
     * Helper.
43
     *
44
     * @var Helper
45
     */
46
    private $helper;
47
48
    /**
49
     * Cache.
50
     *
51
     * @var Cache
52
     */
53
    private $cache;
54
55
    /**
56
     * @var object
57
     */
58
    private $config;
59
60
    /**
61
     * Repository constructor.
62
     *
63
     * @param CacheContract $cache
64
     * @param Hydrator $hydrator
65
     * @param Helper $helper
66
     * @param object $config
67
     */
68 34
    public function __construct(CacheContract $cache, Hydrator $hydrator, Helper $helper, $config)
69
    {
70 34
        $this->cache = $cache;
0 ignored issues
show
Documentation Bug introduced by
$cache is of type object<Psr\SimpleCache\CacheInterface>, but the property $cache was declared to be of type object<PragmaRX\Countrie...Services\Cache\Service>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
71
72 34
        $this->hydrator = $hydrator;
73
74 34
        $this->helper = $helper;
75
76 34
        $this->config = $config;
77 34
    }
78
79
    /**
80
     * Call magic method.
81
     *
82
     * @param $name
83
     * @param array $arguments
84
     * @return mixed
85
     */
86 27
    public function __call($name, array $arguments = [])
87
    {
88 27
        return \call_user_func_array([$this->all(), $name], $arguments);
89
    }
90
91
    /**
92
     * Call a method currencies collection.
93
     *
94
     * @param $name
95
     * @param $arguments
96
     * @return bool|mixed
97
     */
98 32
    public function call($name, $arguments)
99
    {
100 32
        if ($value = $this->cache->get($cacheKey = $this->cache->makeKey([$name, $arguments]))) {
101
            return $value;
102
        }
103
104 32
        $result = \call_user_func_array([$this, $name], $arguments);
105
106 32
        if ($this->config->get('hydrate.before')) {
107 32
            $result = $this->hydrator->hydrate($result);
108
        }
109
110 32
        return $this->cache->set($cacheKey, $result, $this->config->get('cache.duration'));
111
    }
112
113
    /**
114
     * @return Helper
115
     */
116 9
    public function getHelper()
117
    {
118 9
        return $this->helper;
119
    }
120
121
    /**
122
     * Hydrator getter.
123
     *
124
     * @return Hydrator
125
     */
126 3
    public function getHydrator()
127
    {
128 3
        return $this->hydrator;
129
    }
130
131
    /**
132
     * Boot the repository.
133
     * @return static
134
     */
135 34
    public function boot()
136
    {
137 34
        $this->loadCountries();
138
139 34
        return $this;
140
    }
141
142
    /**
143
     * Load countries.
144
     *
145
     * @return \PragmaRX\Coollection\Package\Coollection
146
     */
147 34
    public function loadCountries()
148
    {
149 34
        $this->countriesJson = $this->loadCountriesJson();
150
151
        $overload = $this->helper->loadJsonFiles($this->helper->dataDir('countries/overload'))->mapWithKeys(function ($country, $code) {
152 34
            return [Str::upper($code) => $country];
153 34
        });
154
155 34
        $this->countriesJson = $this->countriesJson->overwrite($overload);
156
157 34
        return $this->countriesJson;
158
    }
159
160
    /**
161
     * Load countries json file.
162
     * @return \PragmaRX\Coollection\Package\Coollection
163
     * @throws \Exception
164
     */
165 34
    public function loadCountriesJson()
166
    {
167 34
        $data = $this->helper->loadJson(
168 34
            $fileName = $this->helper->dataDir('countries/default/_all_countries.json')
169
        );
170
171 34
        if ($data->isEmpty()) {
172
            throw new \Exception("Could not load countries from {$fileName}");
173
        }
174
175 34
        return $data;
176
    }
177
178
    /**
179
     * Load currency json file.
180
     *
181
     * @param $code
182
     * @return string
183
     */
184 6
    public function loadCurrenciesForCountry($code)
185
    {
186 6
        $currencies = $this->helper->loadJson(
187 6
            $this->helper->dataDir('currencies/default/'.strtolower($code).'.json')
188
        );
189
190 6
        return $currencies;
191
    }
192
193
    /**
194
     * Get all countries.
195
     *
196
     * @return \PragmaRX\Coollection\Package\Coollection
197
     */
198 32
    public function all()
199
    {
200 32
        return countriesCollect($this->countriesJson);
201
    }
202
203
    /**
204
     * Get all currencies.
205
     *
206
     * @return \PragmaRX\Coollection\Package\Coollection
207
     */
208 1
    public function currencies()
209
    {
210
        $currencies = $this->helper->loadJsonFiles($this->helper->dataDir('currencies/default'))->mapWithKeys(function ($country, $code) {
211 1
            return [Str::upper($code) => $country];
212 1
        });
213
214
        $overload = $this->helper->loadJsonFiles($this->helper->dataDir('currencies/overload'))->mapWithKeys(function ($country, $code) {
215
            return [Str::upper($code) => $country];
216 1
        });
217
218 1
        return $currencies->overwrite($overload);
219
    }
220
221
    /**
222
     * Make flags array for a coutry.
223
     *
224
     * @param $country
225
     * @return array
226
     */
227 31
    public function makeAllFlags($country)
228
    {
229
        return [
230
            // https://www.flag-sprites.com/
231
            // https://github.com/LeoColomb/flag-sprites
232 31
            'sprite' => '<span class="flag flag-'.($cca3 = strtolower($country['cca3'])).'"></span>',
233
234
            // https://github.com/lipis/flag-icon-css
235 31
            'flag-icon' => '<span class="flag-icon flag-icon-'.($iso_a2 = strtolower($country['iso_a2'])).'"></span>',
236 31
            'flag-icon-squared' => '<span class="flag-icon flag-icon-'.$iso_a2.' flag-icon-squared"></span>',
237
238
            // https://github.com/lafeber/world-flags-sprite
239 31
            'world-flags-sprite' => '<span class="flag '.$cca3.'"></span>',
240
241
            // Internal svg file
242 31
            'svg' => $this->getFlagSvg($country['cca3']),
243
244 31
            'svg_path' => $this->getFlagSvgPath($country['cca3']),
245
        ];
246
    }
247
248
    /**
249
     * Read the flag SVG file.
250
     *
251
     * @param $country
252
     * @return string
253
     */
254 31
    public function getFlagSvg($country)
255
    {
256 31
        return $this->helper->loadFile(
257 31
            $this->getFlagSvgPath($country)
258
        );
259
    }
260
261
    /**
262
     * Get the SVG file path.
263
     *
264
     * @param $country
265
     * @return string
266
     */
267 31
    public function getFlagSvgPath($country)
268
    {
269 31
        return $this->helper->dataDir('flags/'.strtolower($country).'.svg');
270
    }
271
272
    /**
273
     * Get country geometry.
274
     *
275
     * @param $country
276
     * @return string
277
     */
278 1
    public function getGeometry($country)
279
    {
280 1
        return $this->helper->loadFile(
281 1
            $this->helper->dataDir('geo/'.strtolower($country).'.geo.json')
282
        );
283
    }
284
285
    /**
286
     * Get country topology.
287
     *
288
     * @param $country
289
     * @return string
290
     */
291 1
    public function getTopology($country)
292
    {
293 1
        return $this->helper->loadFile(
294 1
            $this->helper->dataDir('topo/'.strtolower($country).'.topo.json')
295
        );
296
    }
297
298
    /**
299
     * Hydrate a country element.
300
     *
301
     * @param $collection
302
     * @param null $elements
303
     * @return \PragmaRX\Coollection\Package\Coollection
304
     */
305 29
    public function hydrate($collection, $elements = null)
306
    {
307 29
        return $this->hydrator->hydrate($collection, $elements);
308
    }
309
310
    /**
311
     * Find a country timezone.
312
     *
313
     * @param $countryCode
314
     * @return null
315
     */
316 5
    public function findTimezones($countryCode)
317
    {
318 5
        return $this->helper->loadJson(
319 5
            $this->helper->dataDir('timezones/countries/default/'.strtolower($countryCode).'.json')
320
        );
321
    }
322
323
    /**
324
     * Find a country timezone.
325
     *
326
     * @param $zoneId
327
     * @return \PragmaRX\Coollection\Package\Coollection
328
     */
329 2
    public function findTimezoneTime($zoneId)
330
    {
331 2
        return $this->helper->loadJson(
332 2
            $this->helper->dataDir("timezones/timezones/default/{$zoneId}.json")
333
        );
334
    }
335
}
336