Completed
Push — master ( 0820ba...224783 )
by ARCANEDEV
8s
created

AbstractService::getDefaultCurrency()   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\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(
147
                $this->getSupported(),
148
                [$this->getDefault()]
149
            );
150
        }
151
152
        return $to;
153
    }
154
155
    /**
156
     * Set the cache configs.
157
     *
158
     * @param  array  $configs
159
     */
160 60
    protected function setCacheConfigs(array $configs)
161
    {
162 60
        $this->cacheEnabled  = Arr::get($configs, 'cache.enabled', false);
163 60
        $this->cacheKey      = Arr::get($configs, 'cache.key', 'currencies.rates');
164 60
        $this->cacheDuration = Arr::get($configs, 'cache.duration', 0);
165 60
    }
166
167
    /**
168
     * Set the configs.
169
     *
170
     * @param  array  $configs
171
     */
172
    abstract protected function setProviderConfigs(array $configs);
173
174
    /**
175
     * Get cache key.
176
     *
177
     * @return string
178
     */
179
    protected function getCacheKey()
180
    {
181
        return $this->cacheKey;
182
    }
183
184
    /**
185
     * Check if cache is enabled.
186
     *
187
     * @return bool
188
     */
189 24
    protected function isCacheEnabled()
190
    {
191 24
        return (bool) $this->cacheEnabled;
192
    }
193
194
    /* ------------------------------------------------------------------------------------------------
195
     |  Main Functions
196
     | ------------------------------------------------------------------------------------------------
197
     */
198
    /**
199
     * Get currencies rates.
200
     *
201
     * @return \Arcanedev\Currencies\Entities\RateCollection
202
     */
203 24
    public function rates()
204
    {
205 24
        $from     = $this->getFromCurrency();
206 24
        $self     = $this;
207
        $callback = function () use ($self, $from) {
208 24
            return $self->request($from);
209 24
        };
210
211 24
        $rates = $this->isCacheEnabled()
212 18
            ? $this->cacheResults($callback)
213 24
            : call_user_func($callback);
214
215 24
        return $this->prepareRates($from, $rates);
216
    }
217
218
    /**
219
     * Get supported currencies rates.
220
     *
221
     * @return \Arcanedev\Currencies\Entities\RateCollection
222
     */
223 12
    public function supportedRates()
224
    {
225 12
        $supported = $this->getSupported();
226 12
        $rates     = $this->rates();
227 12
        $from      = $rates->getFrom();
228
229 12
        return $rates->filter(function (RateContract $rate) use ($supported) {
230 12
            return in_array($rate->to(), $supported);
231 12
        })->setFrom($from);
232
    }
233
234
    /* ------------------------------------------------------------------------------------------------
235
     |  Other Functions
236
     | ------------------------------------------------------------------------------------------------
237
     */
238
    /**
239
     * Make an API request.
240
     *
241
     * @param  string  $from
242
     * @param  array   $to
243
     *
244
     * @return \Arcanedev\Currencies\Entities\RateCollection
245
     */
246
    abstract protected function request($from, array $to = []);
247
248
    /**
249
     * Cache results.
250
     *
251
     * @param  Closure  $callback
252
     *
253
     * @return array
254
     */
255
    private function cacheResults(Closure $callback)
256
    {
257
        return $this->cache->remember(
258
            $this->getCacheKey(),
259
            $this->cacheDuration,
260
            $callback
261
        );
262
    }
263
264
    /**
265
     * Prepare rates collection.
266
     *
267
     * @param  string  $from
268
     * @param  array   $rates
269
     *
270
     * @return \Arcanedev\Currencies\Entities\RateCollection
271
     */
272 24
    protected function prepareRates($from, array $rates)
273
    {
274 24
        return RateCollection::make()->load($from, $rates);
275
    }
276
}
277