Completed
Push — master ( 224783...8539d8 )
by ARCANEDEV
09:04
created

AbstractService::cacheResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
cc 1
eloc 5
nc 1
nop 1
ccs 0
cts 5
cp 0
crap 2
rs 9.4285
1
<?php namespace Arcanedev\Currencies\Services;
2
3
use Arcanedev\Currencies\Contracts\CurrencyManager as CurrencyManagerContract;
4
use Arcanedev\Currencies\Contracts\Entities\Rate as RateContract;
5
use Arcanedev\Currencies\Contracts\Http\Client as ClientContract;
6
use Arcanedev\Currencies\Contracts\Services\CurrencyService;
7
use Arcanedev\Currencies\Entities\RateCollection;
8
use Closure;
9
use Illuminate\Contracts\Cache\Repository as CacheContract;
10
use Illuminate\Support\Arr;
11
12
/**
13
 * Class     AbstractService
14
 *
15
 * @package  Arcanedev\Currencies\Services
16
 * @author   ARCANEDEV <[email protected]>
17
 */
18
abstract class AbstractService implements CurrencyService
19
{
20
    /* ------------------------------------------------------------------------------------------------
21
     |  Properties
22
     | ------------------------------------------------------------------------------------------------
23
     */
24
    /**
25
     * The base URL.
26
     *
27
     * @var string
28
     */
29
    protected $baseUrl = '';
30
31
    /**
32
     * @var \Arcanedev\Currencies\Contracts\CurrencyManager
33
     */
34
    protected $manager;
35
36
    /**
37
     * @var \Arcanedev\Currencies\Contracts\Http\Client
38
     */
39
    protected $client;
40
41
    /**
42
     * The cache repository.
43
     *
44
     * @var \Illuminate\Contracts\Cache\Repository
45
     */
46
    protected $cache;
47
48
    /**
49
     * The enabled cache status.
50
     *
51
     * @var bool
52
     */
53
    protected $cacheEnabled;
54
55
    /**
56
     * The cache key name.
57
     *
58
     * @var string
59
     */
60
    protected $cacheKey;
61
62
    /**
63
     * The cache duration.
64
     *
65
     * @var int
66
     */
67
    protected $cacheDuration;
68
69
    /* ------------------------------------------------------------------------------------------------
70
     |  Constructor
71
     | ------------------------------------------------------------------------------------------------
72
     */
73
    /**
74
     * AbstractService constructor.
75
     *
76
     * @param  \Arcanedev\Currencies\Contracts\CurrencyManager  $manager
77
     * @param  \Arcanedev\Currencies\Contracts\Http\Client      $client
78
     * @param  \Illuminate\Contracts\Cache\Repository           $cache
79
     * @param  array                                            $configs
80
     */
81 60
    public function __construct(
82
        CurrencyManagerContract $manager,
83
        ClientContract $client,
84
        CacheContract $cache,
85
        array $configs = []
86
    ) {
87 60
        $this->manager = $manager;
88 60
        $this->client  = $client;
89 60
        $this->cache   = $cache;
90 60
        $this->setCacheConfigs($configs);
91 60
        $this->setProviderConfigs($configs);
92
93 60
        $this->client->setBaseUrl($this->baseUrl);
94 60
    }
95
96
    /* ------------------------------------------------------------------------------------------------
97
     |  Getters & Setters
98
     | ------------------------------------------------------------------------------------------------
99
     */
100
    /**
101
     * Get the default currency.
102
     *
103
     * @return string
104
     */
105 24
    public function getDefault()
106
    {
107 24
        return $this->manager->getDefault();
108
    }
109
110
    /**
111
     * Get the supported currencies.
112
     *
113
     * @return array
114
     */
115 12
    public function getSupported()
116
    {
117 12
        return $this->manager->getSupported();
118
    }
119
120
    /**
121
     * Get the `from` currency.
122
     *
123
     * @param  string|null  $from
124
     *
125
     * @return string
126
     */
127 24
    protected function getFromCurrency($from = null)
128
    {
129 24
        if (is_null($from)) {
130 24
            $from = $this->getDefault();
131 18
        }
132
133 24
        return $from;
134
    }
135
136
    /**
137
     * Get the `to` currencies.
138
     *
139
     * @param  array|string|null  $to
140
     *
141
     * @return array
142
     */
143
    protected function getToCurrencies($to = null)
144
    {
145
        if (is_null($to)) {
146
            return array_diff($this->getSupported(), [$this->getDefault()]);
147
        }
148
149
        return is_array($to) ? $to : [$to];
150
    }
151
152
    /**
153
     * Set the cache configs.
154
     *
155
     * @param  array  $configs
156
     */
157 60
    protected function setCacheConfigs(array $configs)
158
    {
159 60
        $this->cacheEnabled  = Arr::get($configs, 'cache.enabled', false);
160 60
        $this->cacheKey      = Arr::get($configs, 'cache.key', 'currencies.rates');
161 60
        $this->cacheDuration = Arr::get($configs, 'cache.duration', 0);
162 60
    }
163
164
    /**
165
     * Set the configs.
166
     *
167
     * @param  array  $configs
168
     */
169
    abstract protected function setProviderConfigs(array $configs);
170
171
    /**
172
     * Get cache key.
173
     *
174
     * @return string
175
     */
176
    protected function getCacheKey()
177
    {
178
        return $this->cacheKey;
179
    }
180
181
    /**
182
     * Check if cache is enabled.
183
     *
184
     * @return bool
185
     */
186 24
    protected function isCacheEnabled()
187
    {
188 24
        return (bool) $this->cacheEnabled;
189
    }
190
191
    /* ------------------------------------------------------------------------------------------------
192
     |  Main Functions
193
     | ------------------------------------------------------------------------------------------------
194
     */
195
    /**
196
     * Get currencies rates.
197
     *
198
     * @return \Arcanedev\Currencies\Entities\RateCollection
199
     */
200 24
    public function rates()
201
    {
202 24
        $from     = $this->getFromCurrency();
203 24
        $self     = $this;
204
        $callback = function () use ($self, $from) {
205 24
            return $self->request($from);
206 24
        };
207
208 24
        $rates = $this->isCacheEnabled()
209 18
            ? $this->cacheResults($callback)
210 24
            : call_user_func($callback);
211
212 24
        return $this->prepareRates($from, $rates);
213
    }
214
215
    /**
216
     * Get supported currencies rates.
217
     *
218
     * @return \Arcanedev\Currencies\Entities\RateCollection
219
     */
220 12
    public function supportedRates()
221
    {
222 12
        $supported = $this->getSupported();
223 12
        $rates     = $this->rates();
224 12
        $from      = $rates->getFrom();
225
226 12
        return $rates->filter(function (RateContract $rate) use ($supported) {
227 12
            return in_array($rate->to(), $supported);
228 12
        })->setFrom($from);
229
    }
230
231
    /* ------------------------------------------------------------------------------------------------
232
     |  Other Functions
233
     | ------------------------------------------------------------------------------------------------
234
     */
235
    /**
236
     * Make an API request.
237
     *
238
     * @param  string  $from
239
     * @param  array   $to
240
     *
241
     * @return \Arcanedev\Currencies\Entities\RateCollection
242
     */
243
    abstract protected function request($from, array $to = []);
244
245
    /**
246
     * Cache results.
247
     *
248
     * @param  Closure  $callback
249
     *
250
     * @return array
251
     */
252
    private function cacheResults(Closure $callback)
253
    {
254
        return $this->cache->remember(
255
            $this->getCacheKey(),
256
            $this->cacheDuration,
257
            $callback
258
        );
259
    }
260
261
    /**
262
     * Prepare rates collection.
263
     *
264
     * @param  string  $from
265
     * @param  array   $rates
266
     *
267
     * @return \Arcanedev\Currencies\Entities\RateCollection
268
     */
269 24
    protected function prepareRates($from, array $rates)
270
    {
271 24
        return RateCollection::make()->load($from, $rates);
272
    }
273
}
274