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

AbstractApiService::request()

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\Services;
2
3
use Arcanedev\Currencies\Bases\AbstractService;
4
use Arcanedev\Currencies\Contracts\CurrencyManager as CurrencyManagerContract;
5
use Arcanedev\Currencies\Contracts\Http\Client as ClientContract;
6
use Illuminate\Contracts\Cache\Repository as CacheContract;
7
8
/**
9
 * Class     AbstractApiService
10
 *
11
 * @package  Arcanedev\Currencies\Services
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
abstract class AbstractApiService extends AbstractService
15
{
16
    /* ------------------------------------------------------------------------------------------------
17
     |  Properties
18
     | ------------------------------------------------------------------------------------------------
19
     */
20
    /**
21
     * The base URL.
22
     *
23
     * @var string
24
     */
25
    protected $baseUrl = '';
26
27
    /**
28
     * @var \Arcanedev\Currencies\Contracts\Http\Client
29
     */
30
    protected $client;
31
32
    /* ------------------------------------------------------------------------------------------------
33
     |  Constructor
34
     | ------------------------------------------------------------------------------------------------
35
     */
36
    /**
37
     * AbstractApiService constructor.
38
     *
39
     * @param  \Arcanedev\Currencies\Contracts\CurrencyManager  $manager
40
     * @param  \Arcanedev\Currencies\Contracts\Http\Client      $client
41
     * @param  \Illuminate\Contracts\Cache\Repository           $cache
42
     * @param  array                                            $configs
43
     */
44 96
    public function __construct(
45
        CurrencyManagerContract $manager,
46
        CacheContract $cache,
47
        array $configs,
48
        ClientContract $client
49
    ) {
50 96
        $this->client  = $client;
51 96
        $this->client->setBaseUrl($this->baseUrl);
52
53 96
        parent::__construct($manager, $cache, $configs);
54 96
    }
55
56
    /* ------------------------------------------------------------------------------------------------
57
     |  Main Functions
58
     | ------------------------------------------------------------------------------------------------
59
     */
60
    /**
61
     * Get currencies rates.
62
     *
63
     * @return \Arcanedev\Currencies\Entities\RateCollection
64
     */
65 48
    public function rates()
66
    {
67 48
        $from     = $this->getFromCurrency();
68 48
        $self     = $this;
69 48
        $callback = function () use ($self, $from) {
70 48
            return $self->request($from);
71 48
        };
72
73 48
        $rates = $this->isCacheEnabled()
74 36
            ? $this->cacheResults($callback)
75 48
            : call_user_func($callback);
76
77 48
        return $this->makeRatesCollection($from, $rates);
78
    }
79
80
    /* ------------------------------------------------------------------------------------------------
81
     |  Other Functions
82
     | ------------------------------------------------------------------------------------------------
83
     */
84
    /**
85
     * Make an API request.
86
     *
87
     * @param  string  $from
88
     * @param  array   $to
89
     *
90
     * @return \Arcanedev\Currencies\Entities\RateCollection
91
     */
92
    abstract protected function request($from, array $to = []);
93
}
94