Completed
Pull Request — master (#10)
by ARCANEDEV
08:43
created

AbstractService::getSupported()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
cc 1
eloc 2
nc 1
nop 0
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php namespace Arcanedev\Currencies\Services;
2
3
use Arcanedev\Currencies\Contracts\CurrencyManager as CurrencyManagerContract;
4
use Arcanedev\Currencies\Contracts\Http\Client as ClientContract;
5
use Arcanedev\Currencies\Contracts\Services\CurrencyService;
6
use Arcanedev\Currencies\Entities\RateCollection;
7
use Closure;
8
use Illuminate\Contracts\Cache\Repository as CacheContract;
9
use Illuminate\Support\Arr;
10
11
/**
12
 * Class     AbstractService
13
 *
14
 * @package  Arcanedev\Currencies\Services
15
 * @author   ARCANEDEV <[email protected]>
16
 */
17
abstract class AbstractService implements CurrencyService
18
{
19
    /* ------------------------------------------------------------------------------------------------
20
     |  Properties
21
     | ------------------------------------------------------------------------------------------------
22
     */
23
    /**
24
     * The base URL.
25
     *
26
     * @var string
27
     */
28
    protected $baseUrl = '';
29
30
    /**
31
     * @var \Arcanedev\Currencies\Contracts\CurrencyManager
32
     */
33
    protected $manager;
34
35
    /**
36
     * @var \Arcanedev\Currencies\Contracts\Http\Client
37
     */
38
    protected $client;
39
40
    /**
41
     * The cache repository.
42
     *
43
     * @var \Illuminate\Contracts\Cache\Repository
44
     */
45
    protected $cache;
46
47
    /**
48
     * The enabled cache status.
49
     *
50
     * @var bool
51
     */
52
    protected $cacheEnabled;
53
54
    /**
55
     * The cache key name.
56
     *
57
     * @var string
58
     */
59
    protected $cacheKey;
60
61
    /**
62
     * The cache duration.
63
     *
64
     * @var int
65
     */
66
    protected $cacheDuration;
67
68
    /* ------------------------------------------------------------------------------------------------
69
     |  Constructor
70
     | ------------------------------------------------------------------------------------------------
71
     */
72
    /**
73
     * AbstractService constructor.
74
     *
75
     * @param  \Arcanedev\Currencies\Contracts\CurrencyManager  $manager
76
     * @param  \Arcanedev\Currencies\Contracts\Http\Client      $client
77
     * @param  \Illuminate\Contracts\Cache\Repository           $cache
78
     * @param  array                                            $configs
79
     */
80 48
    public function __construct(
81
        CurrencyManagerContract $manager,
82
        ClientContract $client,
83
        CacheContract $cache,
84
        array $configs = []
85
    ) {
86 48
        $this->manager = $manager;
87 48
        $this->client  = $client;
88 48
        $this->cache   = $cache;
89 48
        $this->setCacheConfigs($configs);
90 48
        $this->setProviderConfigs($configs);
91
92 48
        $this->client->setBaseUrl($this->baseUrl);
93 48
    }
94
95
    /* ------------------------------------------------------------------------------------------------
96
     |  Getters & Setters
97
     | ------------------------------------------------------------------------------------------------
98
     */
99
    /**
100
     * Get the default currency.
101
     *
102
     * @return string
103
     */
104 12
    public function getDefault()
105
    {
106 12
        return $this->manager->getDefault();
107
    }
108
109
    /**
110
     * Get the supported currencies.
111
     *
112
     * @return array
113
     */
114 12
    public function getSupported()
115
    {
116 12
        return $this->manager->getSupported();
117
    }
118
119
    /**
120
     * Get the `from` currency.
121
     *
122
     * @param  string  $from
123
     *
124
     * @return string
125
     */
126 12
    protected function getFromCurrency($from)
127
    {
128 12
        if (is_null($from)) {
129 12
            $from = $this->getDefault();
130 9
        }
131
132 12
        return $from;
133
    }
134
135
    /**
136
     * Get the `to` currencies.
137
     *
138
     * @param  array|string|null  $to
139
     *
140
     * @return array
141
     */
142 12
    protected function getToCurrencies($to)
143
    {
144 12
        if (is_null($to)) {
145 12
            return array_diff(
146 12
                $this->getSupported(),
147 12
                [$this->getDefault()]
148 9
            );
149
        }
150
151
        return $to;
152
    }
153
154
    /**
155
     * Set the cache configs.
156
     *
157
     * @param  array  $configs
158
     */
159 48
    protected function setCacheConfigs(array $configs)
160
    {
161 48
        $this->cacheEnabled  = Arr::get($configs, 'cache.enabled', false);
162 48
        $this->cacheKey      = Arr::get($configs, 'cache.key', 'currencies.rates');
163 48
        $this->cacheDuration = Arr::get($configs, 'cache.duration', 0);
164 48
    }
165
166
    /**
167
     * Set the configs.
168
     *
169
     * @param  array  $configs
170
     */
171
    abstract protected function setProviderConfigs(array $configs);
172
173
    /**
174
     * Get cache key.
175
     *
176
     * @return string
177
     */
178
    protected function getCacheKey()
179
    {
180
        return $this->cacheKey;
181
    }
182
183
    /**
184
     * Check if cache is enabled.
185
     *
186
     * @return bool
187
     */
188 12
    protected function isCacheEnabled()
189
    {
190 12
        return (bool) $this->cacheEnabled;
191
    }
192
193
    /* ------------------------------------------------------------------------------------------------
194
     |  Main Functions
195
     | ------------------------------------------------------------------------------------------------
196
     */
197
    /**
198
     * Get currencies rates.
199
     *
200
     * @param  string|null        $from
201
     * @param  array|string|null  $to
202
     *
203
     * @return \Arcanedev\Currencies\Entities\RateCollection
204
     */
205 12
    public function rates($from = null, $to = null)
206
    {
207 12
        $from = $this->getFromCurrency($from);
208 12
        $to   = $this->getToCurrencies($to);
209
210 12
        $self     = $this;
211 12
        $callback = function () use ($self, $from, $to) {
212 12
            return $self->request($from, $to);
0 ignored issues
show
Bug introduced by
It seems like $to defined by $this->getToCurrencies($to) on line 208 can also be of type string; however, Arcanedev\Currencies\Ser...tractService::request() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
213 12
        };
214
215 12
        $rates = $this->isCacheEnabled()
216 9
            ? $this->cacheResults($callback)
217 12
            : call_user_func($callback);
218
219 12
        return $this->prepareRates($from, $rates);
220
    }
221
222
    /**
223
     * Make an API request.
224
     *
225
     * @param  string  $from
226
     * @param  array   $to
227
     *
228
     * @return \Arcanedev\Currencies\Entities\RateCollection
229
     */
230
    abstract protected function request($from, array $to);
231
232
    /**
233
     * Cache results.
234
     *
235
     * @param  Closure  $callback
236
     *
237
     * @return array
238
     */
239
    private function cacheResults(Closure $callback)
240
    {
241
        return $this->cache->remember(
242
            $this->getCacheKey(),
243
            $this->cacheDuration,
244
            $callback
245
        );
246
    }
247
248
    /**
249
     * Prepare rates collection.
250
     *
251
     * @param  string  $from
252
     * @param  array   $rates
253
     *
254
     * @return \Arcanedev\Currencies\Entities\RateCollection
255
     */
256 12
    protected function prepareRates($from, array $rates)
257
    {
258 12
        return RateCollection::make()->load($from, $rates);
259
    }
260
}
261