Completed
Pull Request — master (#8)
by ARCANEDEV
03:31
created

DriverManager   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 125
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultDriver() 0 4 1
A getDriverClass() 0 4 1
A getDriverOptions() 0 4 1
A getConfig() 0 4 1
A config() 0 4 1
A createIpApiDriver() 0 4 1
A createFreegeoipDriver() 0 4 1
A createMaxmindDatabaseDriver() 0 4 1
A createMaxmindApiDriver() 0 4 1
A buildDriver() 0 6 1
1
<?php namespace Arcanedev\GeoIP;
2
3
use Arcanedev\GeoIP\Contracts\DriverFactory;
4
use Illuminate\Support\Manager;
5
6
/**
7
 * Class     DriverManager
8
 *
9
 * @package  Arcanedev\GeoIP
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class DriverManager extends Manager implements DriverFactory
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Getters & Setters
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /**
19
     * Get the default driver name.
20
     *
21
     * @return string
22
     */
23 21
    public function getDefaultDriver()
24
    {
25 21
        return $this->getConfig('default');
26
    }
27
28
    /**
29
     * Get the driver class.
30
     *
31
     * @param  string  $key
32
     *
33
     * @return string
34
     */
35 45
    private function getDriverClass($key)
36
    {
37 45
        return $this->getConfig("supported.$key.driver");
38
    }
39
40
    /**
41
     * Get the driver options.
42
     *
43
     * @param  string  $key
44
     *
45
     * @return array
46
     */
47 45
    private function getDriverOptions($key)
48
    {
49 45
        return $this->getConfig("supported.$key.options", []);
50
    }
51
52
    /**
53
     * Get the config.
54
     *
55
     * @param  string      $key
56
     * @param  mixed|null  $default
57
     *
58
     * @return mixed
59
     */
60 45
    public function getConfig($key, $default = null)
61
    {
62 45
        return $this->config()->get("geoip.$key", $default);
63
    }
64
65
    /**
66
     * Get the config instance.
67
     *
68
     * @return \Illuminate\Contracts\Config\Repository
69
     */
70 45
    private function config()
71
    {
72 45
        return $this->app['config'];
73
    }
74
75
    /* ------------------------------------------------------------------------------------------------
76
     |  Main Functions
77
     | ------------------------------------------------------------------------------------------------
78
     */
79
    /**
80
     * Build the 'ip-api' driver.
81
     *
82
     * @return Drivers\IpApiDriver
83
     */
84 9
    protected function createIpApiDriver()
85
    {
86 9
        return $this->buildDriver('ip-api');
87
    }
88
89
    /**
90
     * Build the 'freegeoip' driver.
91
     *
92
     * @return Drivers\FreeGeoIpDriver
93
     */
94 30
    protected function createFreegeoipDriver()
95
    {
96 30
        return $this->buildDriver('freegeoip');
97
    }
98
99
    /**
100
     * Get the 'maxmind-database' driver.
101
     *
102
     * @return mixed
103
     */
104 12
    protected function createMaxmindDatabaseDriver()
105
    {
106 12
        return $this->buildDriver('maxmind-database');
107
    }
108
109
    /**
110
     * Get the 'maxmind-api' driver.
111
     *
112
     * @return Drivers\MaxmindApiDriver
113
     */
114 3
    protected function createMaxmindApiDriver()
115
    {
116 3
        return $this->buildDriver('maxmind-api');
117
    }
118
119
    /* ------------------------------------------------------------------------------------------------
120
     |  Other Functions
121
     | ------------------------------------------------------------------------------------------------
122
     */
123
    /**
124
     * Build the driver.
125
     *
126
     * @param  string  $key
127
     *
128
     * @return mixed
129
     */
130 45
    private function buildDriver($key)
131
    {
132 45
        $class = $this->getDriverClass($key);
133
134 45
        return new $class($this->getDriverOptions($key));
135
    }
136
}
137