Completed
Push — master ( c8ea86...04bb20 )
by ARCANEDEV
05:10 queued 05:02
created

GeoIPServiceProvider::getBasePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Arcanedev\GeoIP;
2
3
use Arcanedev\Support\PackageServiceProvider;
4
use Illuminate\Support\Arr;
5
6
/**
7
 * Class     GeoIPServiceProvider
8
 *
9
 * @package  Arcanedev\GeoIP
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class GeoIPServiceProvider extends PackageServiceProvider
13
{
14
    /* -----------------------------------------------------------------
15
     |  Properties
16
     | -----------------------------------------------------------------
17
     */
18
19
    /**
20
     * Package name.
21
     *
22
     * @var string
23
     */
24
    protected $package = 'geoip';
25
26
    /**
27
     * Indicates if loading of the provider is deferred.
28
     *
29
     * @var bool
30
     */
31
    protected $defer = true;
32
33
    /* -----------------------------------------------------------------
34
     |  Main Methods
35
     | -----------------------------------------------------------------
36
     */
37
38
    /**
39
     * Register the service provider.
40
     */
41 66
    public function register()
42
    {
43 66
        parent::register();
44
45 66
        $this->registerConfig();
46
47 66
        $this->registerGeoIpManager();
48 66
        $this->registerGeoIpCache();
49 66
        $this->registerGeoIp();
50
51 66
        $this->registerConsoleServiceProvider(Providers\CommandServiceProvider::class);
52 66
    }
53
54
    /**
55
     * Boot the service provider.
56
     */
57 66
    public function boot()
58
    {
59 66
        parent::boot();
60
61 66
        $this->publishConfig();
62 66
    }
63
64
    /**
65
     * Get the services provided by the provider.
66
     *
67
     * @return array
68
     */
69 6
    public function provides()
70
    {
71
        return [
72 6
            Contracts\GeoIP::class,
73 2
            Contracts\DriverFactory::class,
74 2
            Contracts\GeoIPDriver::class,
75 2
            Contracts\GeoIPCache::class,
76 2
            Contracts\DriverFactory::class,
77 2
        ];
78
    }
79
80
    /* -----------------------------------------------------------------
81
     |  Other Methods
82
     | -----------------------------------------------------------------
83
     */
84
85
    /**
86
     * Register the GeoIP manager.
87
     */
88 22
    private function registerGeoIpManager()
89
    {
90 44
        $this->singleton(Contracts\DriverFactory::class, function ($app) {
91 48
            return new DriverManager($app);
92 66
        });
93
94 44
        $this->singleton(Contracts\GeoIPDriver::class, function ($app) {
95
            /** @var  \Arcanedev\GeoIP\Contracts\DriverFactory  $manager */
96 18
            $manager = $app[Contracts\DriverFactory::class];
97
98 18
            return $manager->driver();
99 66
        });
100 66
    }
101
102
    /**
103
     * Register the GeoIP Cache store.
104
     */
105 22
    private function registerGeoIpCache()
106
    {
107 44
        $this->singleton(Contracts\GeoIPCache::class, function ($app) {
108
            /** @var \Illuminate\Contracts\Config\Repository $config */
109 18
            $config = $app['config'];
110
111 18
            return new Cache(
112 18
                $app['cache.store'],
113 18
                $config->get('geoip.cache.tags', []),
114 18
                $config->get('geoip.cache.expires', 30)
115 6
            );
116 66
        });
117 66
    }
118
119
    /**
120
     * Register the GeoIP.
121
     */
122
    private function registerGeoIp()
123
    {
124 66
        $this->singleton(Contracts\GeoIP::class, function ($app) {
125
            /** @var \Illuminate\Contracts\Config\Repository $config */
126 18
            $config = $app['config'];
127
128 18
            return new GeoIP(
129 18
                $app[Contracts\GeoIPDriver::class],
130 18
                $app[Contracts\GeoIPCache::class],
131 18
                Arr::only($config->get('geoip', []), ['cache', 'location', 'currencies'])
132 6
            );
133 66
        });
134 22
    }
135
}
136