Completed
Push — master ( 028b29...cb8c8c )
by Antonio Carlos
07:08
created

Countries::getCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace PragmaRX\Countries\Package\Services;
4
5
use PragmaRX\Countries\Update\Updater;
6
use PragmaRX\Countries\Package\Support\Base;
7
use PragmaRX\Coollection\Package\Coollection;
8
use PragmaRX\Countries\Package\Data\Repository;
9
use PragmaRX\Countries\Package\Services\Cache\Service as Cache;
10
11
class Countries extends Base
12
{
13
    /**
14
     * Countries repository.
15
     *
16
     * @var Repository
17
     */
18
    protected $repository;
19
20
    /**
21
     * Helper.
22
     *
23
     * @var Helper
24
     */
25
    protected $helper;
26
27
    /**
28
     * Config.
29
     *
30
     * @var Config
31
     */
32
    protected $config;
33
34
    /**
35
     * Cache.
36
     *
37
     * @var Cache
38
     */
39
    protected $cache;
40
41
    /**
42
     * Updater.
43
     *
44
     * @var updater
45
     */
46
    private $updater;
47
48
    /**
49
     * @var Hydrator
50
     */
51
    private $hydrator;
52
53
    /**
54
     * Service constructor.
55
     *
56
     * @param object $config
57
     * @param Cache $cache
58
     * @param Helper $helper
59
     * @param Hydrator $hydrator
60
     * @param Repository $repository
61
     */
62 34
    public function __construct(
63
        $config = null,
64
        Cache $cache = null,
65
        Helper $helper = null,
66
        Hydrator $hydrator = null,
67
        Repository $repository = null
68
    ) {
69 34
        $a = new \PragmaRX\Countries\Package\Services\Cache\Service();
0 ignored issues
show
Unused Code introduced by
$a is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
70
71 34
        $this->helper = $this->instantiateHelper($helper);
72
73 34
        $this->config = $this->instantiateConfig($config);
74
75 34
        $this->cache = $this->instantiateCache($cache);
76
77 34
        $this->hydrator = $this->instantiateHydrator($hydrator);
78
79 34
        $this->repository = $this->instantiateRepository($repository);
80
81 34
        $this->hydrator->setRepository($this->repository);
82
83 34
        $this->init();
84 34
    }
85
86
    /**
87
     * Call a method.
88
     *
89
     * @param $name
90
     * @param array $arguments
91
     * @return bool|mixed
92
     */
93 32
    public function __call($name, array $arguments = [])
94
    {
95 32
        $result = $this->repository->call($name, $arguments);
96
97 32
        if ($this->config->get('hydrate.after')) {
98
            $result = $this->repository->hydrate($result);
99
        }
100
101 32
        return $result;
102
    }
103
104 34
    private function createCoollectionMacros()
105
    {
106 34
        $instance = $this;
107
108 34
        Coollection::macro('hydrate', function ($elements = null) use ($instance) {
109 29
            return $instance->hydrate($this, $elements);
0 ignored issues
show
Documentation Bug introduced by
The method hydrate does not exist on object<PragmaRX\Countrie...age\Services\Countries>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
110 34
        });
111
112 34
        foreach (Hydrator::HYDRATORS as $hydrator) {
113 34
            $hydrator = 'hydrate'.studly_case($hydrator);
114
115 34
            Coollection::macro($hydrator, function () use ($hydrator, $instance) {
116 3
                return $instance->getRepository()->getHydrator()->{$hydrator}($this);
117 34
            });
118
        }
119 34
    }
120
121
    /**
122
     * Get all currencies.
123
     *
124
     * @return Coollection
125
     */
126 1
    public function currencies()
127
    {
128 1
        return coollect($this->repository->currencies())->unique()->sort();
129
    }
130
131
    /**
132
     * Cache instance getter.
133
     *
134
     * @return Cache|Config
135
     */
136 34
    public function getCache()
137
    {
138 34
        return $this->cache;
139
    }
140
141
    /**
142
     * Get the config instance.
143
     *
144
     * @return Config
145
     */
146 1
    public function getConfig()
147
    {
148 1
        return $this->config;
149
    }
150
151
    /**
152
     * Repository getter.
153
     *
154
     * @return Repository
155
     */
156 3
    public function getRepository()
157
    {
158 3
        return $this->repository;
159
    }
160
161
    /**
162
     * Initialize class.
163
     */
164 34
    protected function init()
165
    {
166 34
        $this->createCoollectionMacros();
167
168 34
        $this->defineConstants();
169
170 34
        $this->repository->boot();
171 34
    }
172
173
    /**
174
     * Instantiate cache.
175
     *
176
     * @param Cache|null $cache
177
     * @return Cache
178
     */
179 34
    protected function instantiateCache(Cache $cache = null)
180
    {
181 34
        if (is_null($this->cache) || ! is_null($cache)) {
182 34
            $this->cache = ! is_null($cache)
183
                ? $cache
184 34
                : new Cache($this->config);
185
        }
186
187 34
        return $this->cache;
188
    }
189
190
    /**
191
     * Instantiate config.
192
     *
193
     * @param object|null $config
194
     * @return Config
195
     */
196 34
    protected function instantiateConfig($config = null)
197
    {
198 34
        if (is_null($this->config) || ! is_null($config)) {
199 34
            $this->config = ! is_null($config)
200
                ? $config
201 34
                : new Config($this->helper);
0 ignored issues
show
Documentation introduced by
$this->helper is of type object<PragmaRX\Countrie...ackage\Services\Helper>, but the function expects a array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
202
        }
203
204 34
        return $this->config;
205
    }
206
207
    /**
208
     * @param Helper|null $helper
209
     * @return Helper
210
     */
211 34
    protected function instantiateHelper(Helper $helper = null)
212
    {
213 34
        $this->helper = is_null($helper)
214 34
            ? (is_null($this->helper)
215 34
                ? $this->helper = new Helper($this->instantiateConfig())
216 34
                : $this->helper)
217
            : $helper;
218
219 34
        return $this->helper;
220
    }
221
222
    /**
223
     * Instantiate hydrator.
224
     *
225
     * @param Hydrator|null $hydrator
226
     * @return Hydrator
227
     */
228 34
    protected function instantiateHydrator(Hydrator $hydrator = null)
229
    {
230 34
        if (is_null($this->hydrator) || ! is_null($hydrator)) {
231 34
            $this->hydrator = ! is_null($hydrator)
232
                ? $hydrator
233 34
                : new Hydrator($this->config);
234
        }
235
236 34
        return $this->hydrator;
237
    }
238
239
    /**
240
     * @param $repository
241
     * @return Repository
242
     */
243 34
    protected function instantiateRepository($repository)
244
    {
245 34
        if (is_null($repository)) {
246 34
            $repository = new Repository(
247 34
                $this->instantiateCache(),
248 34
                $this->instantiateHydrator(),
249 34
                $this->instantiateHelper(),
250 34
                $this->instantiateConfig()
251
            );
252
        }
253
254 34
        return $repository;
255
    }
256
257
    /**
258
     * @return Updater
259
     */
260
    protected function instantiateUpdater()
261
    {
262
        if (is_null($this->updater)) {
263
            $this->updater = new Updater($this->config, $this->helper);
0 ignored issues
show
Documentation introduced by
$this->helper is of type object<PragmaRX\Countrie...ackage\Services\Helper>, but the function expects a object<PragmaRX\Countries\Update\Helper>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation Bug introduced by
It seems like new \PragmaRX\Countries\...>config, $this->helper) of type object<PragmaRX\Countries\Update\Updater> is incompatible with the declared type object<PragmaRX\Countrie...ckage\Services\updater> of property $updater.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
264
        }
265
266
        return $this->updater;
267
    }
268
}
269