Completed
Push — master ( 12a313...e137a7 )
by ARCANEDEV
7s
created

AbstractService::setConfigs()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
nc 1
dl 0
loc 1
ccs 0
cts 0
cp 0
1
<?php namespace Arcanedev\Currencies\Bases;
2
3
use Arcanedev\Currencies\Contracts\CurrencyManager as CurrencyManagerContract;
4
use Arcanedev\Currencies\Contracts\Entities\Rate as RateContract;
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\Bases
15
 * @author   ARCANEDEV <[email protected]>
16
 */
17
abstract class AbstractService implements CurrencyService
18
{
19
    /* ------------------------------------------------------------------------------------------------
20
     |  Properties
21
     | ------------------------------------------------------------------------------------------------
22
     */
23
    /**
24
     * The currency manager instance.
25
     *
26
     * @var \Arcanedev\Currencies\Contracts\CurrencyManager
27
     */
28
    protected $manager;
29
30
    /**
31
     * The cache repository.
32
     *
33
     * @var \Illuminate\Contracts\Cache\Repository
34
     */
35
    protected $cache;
36
37
    /**
38
     * The enabled cache status.
39
     *
40
     * @var bool
41
     */
42
    protected $cacheEnabled;
43
44
    /**
45
     * The cache key name.
46
     *
47
     * @var string
48
     */
49
    protected $cacheKey;
50
51
    /**
52
     * The cache duration.
53
     *
54
     * @var int
55
     */
56
    protected $cacheDuration;
57
58
    /* ------------------------------------------------------------------------------------------------
59
     |  Constructor
60
     | ------------------------------------------------------------------------------------------------
61
     */
62
    /**
63
     * AbstractApiService constructor.
64
     *
65
     * @param  \Arcanedev\Currencies\Contracts\CurrencyManager  $manager
66
     * @param  \Illuminate\Contracts\Cache\Repository           $cache
67
     * @param  array                                            $configs
68
     */
69 120
    public function __construct(
70
        CurrencyManagerContract $manager,
71
        CacheContract $cache,
72
        array $configs
73
    ) {
74 120
        $this->manager = $manager;
75 120
        $this->cache   = $cache;
76 120
        $this->setConfigs($configs);
77 120
        $this->setCacheConfigs($configs);
78 120
    }
79
80
    /* ------------------------------------------------------------------------------------------------
81
     |  Getters & Setters
82
     | ------------------------------------------------------------------------------------------------
83
     */
84
    /**
85
     * Set the configs.
86
     *
87
     * @param  array  $configs
88
     */
89
    abstract protected function setConfigs(array $configs);
90
91
    /**
92
     * Set the cache configs.
93
     *
94
     * @param  array  $configs
95
     */
96 120
    protected function setCacheConfigs(array $configs)
97
    {
98 120
        $this->cacheEnabled  = Arr::get($configs, 'cache.enabled', false);
99 120
        $this->cacheKey      = Arr::get($configs, 'cache.key', 'currencies.rates');
100 120
        $this->cacheDuration = Arr::get($configs, 'cache.duration', 0);
101 120
    }
102
103
    /**
104
     * Get the default currency.
105
     *
106
     * @return string
107
     */
108 60
    public function getDefault()
109
    {
110 60
        return $this->manager->getDefault();
111
    }
112
113
    /**
114
     * Get the supported currencies.
115
     *
116
     * @return array
117
     */
118 24
    public function getSupported()
119
    {
120 24
        return $this->manager->getSupported();
121
    }
122
123
    /**
124
     * Get the `from` currency.
125
     *
126
     * @param  string|null  $from
127
     *
128
     * @return string
129
     */
130 48
    protected function getFromCurrency($from = null)
131
    {
132 48
        return is_null($from) ? $this->getDefault() : $from;
133
    }
134
135
    /**
136
     * Get the `to` currencies.
137
     *
138
     * @param  array|string|null  $to
139
     *
140
     * @return array
141
     */
142
    protected function getToCurrencies($to = null)
143
    {
144
        if (is_null($to)) {
145
            return array_diff($this->getSupported(), [$this->getDefault()]);
146
        }
147
148
        return is_array($to) ? $to : [$to];
149
    }
150
151
    /**
152
     * Get cache key.
153
     *
154
     * @return string
155
     */
156
    protected function getCacheKey()
157
    {
158
        return $this->cacheKey;
159
    }
160
161
    /**
162
     * Check if cache is enabled.
163
     *
164
     * @return bool
165
     */
166 48
    protected function isCacheEnabled()
167
    {
168 48
        return (bool) $this->cacheEnabled;
169
    }
170
171
    /* ------------------------------------------------------------------------------------------------
172
     |  Main Functions
173
     | ------------------------------------------------------------------------------------------------
174
     */
175
    /**
176
     * Get supported currencies rates.
177
     *
178
     * @return \Arcanedev\Currencies\Entities\RateCollection
179
     */
180 24
    public function supportedRates()
181
    {
182 24
        $supported = $this->getSupported();
183 24
        $rates     = $this->rates();
184 24
        $from      = $rates->getFrom();
185
186 24
        return $rates->filter(function (RateContract $rate) use ($supported) {
187 24
            return in_array($rate->to(), $supported);
188 24
        })->setFrom($from);
189
    }
190
191
    /* ------------------------------------------------------------------------------------------------
192
     |  Other Functions
193
     | ------------------------------------------------------------------------------------------------
194
     */
195
    /**
196
     * Cache results.
197
     *
198
     * @param  \Closure  $callback
199
     *
200
     * @return array
201
     */
202
    protected function cacheResults(Closure $callback)
203
    {
204
        return $this->cache->remember(
205
            $this->getCacheKey(),
206
            $this->cacheDuration,
207
            $callback
208
        );
209
    }
210
211
    /**
212
     * Prepare rates collection.
213
     *
214
     * @param  string  $from
215
     * @param  array   $rates
216
     *
217
     * @return \Arcanedev\Currencies\Entities\RateCollection
218
     */
219 60
    protected function makeRatesCollection($from, array $rates)
220
    {
221 60
        return RateCollection::make()->load($from, $rates);
222
    }
223
}
224