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

ConverterManager::getProviderConfigs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
1
<?php namespace Arcanedev\Currencies;
2
3
use Arcanedev\Currencies\Contracts\ConverterManager as ConverterManagerContract;
4
use Illuminate\Contracts\Cache\Repository as CacheContract;
5
use Illuminate\Support\Manager;
6
7
/**
8
 * Class     ConverterManager
9
 *
10
 * @package  Arcanedev\Currencies
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class ConverterManager extends Manager implements ConverterManagerContract
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Main Functions
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * Get the default driver name.
21
     *
22
     * @throws \InvalidArgumentException
23
     *
24
     * @return string
25
     */
26 12
    public function getDefaultDriver()
27
    {
28 12
        return $this->config()->get('currencies.converters.default');
29
    }
30
31
    /**
32
     * Create an instance of Openexchangerates service.
33
     *
34
     * @return \Arcanedev\Currencies\Contracts\Services\CurrencyService
35
     */
36 24
    protected function createOpenexchangeratesDriver()
37
    {
38 24
        return $this->buildApiProvider(
39 24
            Services\OpenExchangeRatesService::class,
40 24
            $this->getProviderConfigs('openexchangerates')
41 18
        );
42
    }
43
44
    /**
45
     * Create an instance of Currencylayer service.
46
     *
47
     * @return \Arcanedev\Currencies\Contracts\Services\CurrencyService
48
     */
49 12
    protected function createCurrencylayerDriver()
50
    {
51 12
        return $this->buildApiProvider(
52 12
            Services\CurrencyLayerService::class,
53 12
            $this->getProviderConfigs('currencylayer')
54 9
        );
55
    }
56
57
    /**
58
     * Create an instance of Array store.
59
     *
60
     * @return \Arcanedev\Currencies\Stores\ArrayStore
61
     */
62 24
    protected function createArrayDriver()
63
    {
64 24
        return new Stores\ArrayStore(
65 24
            $this->app[Contracts\CurrencyManager::class],
66 24
            $this->app[CacheContract::class],
67 24
            $this->getProviderConfigs('array')
68 18
        );
69
    }
70
71
    /* ------------------------------------------------------------------------------------------------
72
     |  Other Functions
73
     | ------------------------------------------------------------------------------------------------
74
     */
75
    /**
76
     * Get the config repository instance.
77
     *
78
     * @return \Illuminate\Contracts\Config\Repository
79
     */
80 48
    protected function config()
81
    {
82 48
        return $this->app['config'];
83
    }
84
85
    /**
86
     * Get the provider configs.
87
     *
88
     * @param  string  $provider
89
     *
90
     * @return array
91
     */
92 48
    protected function getProviderConfigs($provider)
93
    {
94 48
        $configs          = $this->config()->get("currencies.converters.providers.$provider");
95 48
        $configs['cache'] = $this->config()->get("currencies.converters.cache");
96
97 48
        return $configs;
98
    }
99
100
    /**
101
     * Build the converter provider.
102
     *
103
     * @param  string  $provider
104
     * @param  array   $configs
105
     *
106
     * @return \Arcanedev\Currencies\Contracts\Services\CurrencyService
107
     */
108 24
    protected function buildApiProvider($provider, array $configs)
109
    {
110 24
        return new $provider(
111 24
            $this->app[Contracts\CurrencyManager::class],
112 24
            $this->app[CacheContract::class],
113 18
            $configs,
114 24
            $this->app[Contracts\Http\Client::class]
115 18
        );
116
    }
117
}
118