Completed
Pull Request — master (#2)
by ARCANEDEV
02:44
created

ConverterManager::config()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php namespace Arcanedev\Currencies;
2
3
use Arcanedev\Currencies\Contracts\ConverterManager as ConverterManagerContract;
4
use Illuminate\Support\Manager;
5
use InvalidArgumentException;
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
     * Create an instance of the specified driver.
21
     *
22
     * @return \Arcanedev\Currencies\Contracts\CurrencyConverter
23
     */
24
    protected function createOpenexchangeratesDriver()
25
    {
26
        return $this->buildProvider(
27
            \Arcanedev\Currencies\Converters\OpenExchangeRatesConverter::class,
28
            $this->getProviderConfigs('openexchangerates')
29
        );
30
    }
31
32
    /**
33
     * Get the default driver name.
34
     *
35
     * @throws \InvalidArgumentException
36
     *
37
     * @return string
38
     */
39
    public function getDefaultDriver()
40
    {
41
        return $this->config()->get('currencies.converters.default');
42
    }
43
44
    /* ------------------------------------------------------------------------------------------------
45
     |  Other Functions
46
     | ------------------------------------------------------------------------------------------------
47
     */
48
    /**
49
     * Get the config repository instance.
50
     *
51
     * @return \Illuminate\Contracts\Config\Repository
52
     */
53
    protected function config()
54
    {
55
        return $this->app['config'];
56
    }
57
58
    /**
59
     * Get the provider configs.
60
     *
61
     * @param  string  $provider
62
     *
63
     * @return array
64
     */
65
    protected function getProviderConfigs($provider)
66
    {
67
        $configs          = $this->config()->get("currencies.converters.providers.$provider");
68
        $configs['cache'] = $this->config()->get("currencies.converters.cache");
69
70
        return $configs;
71
    }
72
73
    /**
74
     * Build the converter provider.
75
     *
76
     * @param  string  $provider
77
     * @param  array   $configs
78
     *
79
     * @return \Arcanedev\Currencies\Contracts\CurrencyConverter
80
     */
81
    protected function buildProvider($provider, array $configs)
82
    {
83
        return new $provider(
84
            new \Arcanedev\Currencies\Http\Client,
85
            $this->app['cache']->driver(),
86
            $configs
87
        );
88
    }
89
}
90