Completed
Push — master ( f55ac0...46412d )
by ARCANEDEV
14:05
created

GeoIPServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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
     * Package name.
20
     *
21
     * @var string
22
     */
23
    protected $package = 'geoip';
24
25
    /**
26
     * Indicates if loading of the provider is deferred.
27
     *
28
     * @var bool
29
     */
30
    protected $defer   = true;
31
32
    /* ------------------------------------------------------------------------------------------------
33
     |  Getters & Setters
34
     | ------------------------------------------------------------------------------------------------
35
     */
36
    /**
37
     * Get the base path of the package.
38
     *
39
     * @return string
40
     */
41 132
    public function getBasePath()
42
    {
43 132
        return dirname(__DIR__);
44
    }
45
46
    /* ------------------------------------------------------------------------------------------------
47
     |  Main Functions
48
     | ------------------------------------------------------------------------------------------------
49
     */
50
    /**
51
     * Register the service provider.
52
     */
53 132
    public function register()
54
    {
55 132
        $this->registerConfig();
56
57 132
        $this->registerGeoIpManager();
58 132
        $this->registerGeoIpCache();
59 132
        $this->registerGeoIp();
60
61 132
        if ($this->app->runningInConsole()) {
62 132
            $this->commands([
63 132
                Console\ClearCommand::class,
64 66
                Console\UpdateCommand::class,
65 66
            ]);
66 66
        }
67 132
    }
68
69
    /**
70
     * Boot the service provider.
71
     */
72 132
    public function boot()
73
    {
74 132
        parent::boot();
75
76 132
        $this->publishConfig();
77 132
    }
78
79
    /**
80
     * Get the services provided by the provider.
81
     *
82
     * @return array
83
     */
84 12
    public function provides()
85
    {
86
        return [
87 12
            'geoip',
88 6
            Contracts\GeoIP::class,
89 6
            Contracts\DriverFactory::class,
90 6
            Contracts\GeoIPDriver::class,
91 6
            Contracts\GeoIPCache::class,
92 6
            Contracts\DriverFactory::class,
93
94 6
            Console\ClearCommand::class,
95 6
            Console\UpdateCommand::class,
96 6
        ];
97
    }
98
99
    /* ------------------------------------------------------------------------------------------------
100
     |  Other Functions
101
     | ------------------------------------------------------------------------------------------------
102
     */
103
    /**
104
     * Register the GeoIP manager.
105
     */
106 132
    private function registerGeoIpManager()
107
    {
108 132
        $this->singleton(Contracts\DriverFactory::class, DriverManager::class);
109
110
        $this->singleton(Contracts\GeoIPDriver::class, function ($app) {
111 36
            return $app[Contracts\DriverFactory::class]->driver();
112 132
        });
113 132
    }
114
115
    /**
116
     * Register the GeoIP Cache store.
117
     */
118 132
    private function registerGeoIpCache()
119
    {
120
        $this->singleton(Contracts\GeoIPCache::class, function ($app) {
121
            /** @var \Illuminate\Contracts\Config\Repository $config */
122 36
            $config = $app['config'];
123
124 36
            return new Cache(
125 36
                $this->app['cache.store'],
126 36
                $config->get('geoip.cache.tags', []),
127 36
                $config->get('geoip.cache.expires', 30)
128 18
            );
129 132
        });
130 132
    }
131
132
    /**
133
     * Register the GeoIP.
134
     */
135
    private function registerGeoIp()
136
    {
137 132
        $this->singleton(Contracts\GeoIP::class, function ($app) {
138
            /** @var \Illuminate\Contracts\Config\Repository $config */
139 36
            $config = $app['config'];
140
141 36
            return new GeoIP(
142 36
                $app[Contracts\GeoIPDriver::class],
143 36
                $app[Contracts\GeoIPCache::class],
144 36
                Arr::only($config->get('geoip', []), ['cache', 'location', 'currencies'])
145 18
            );
146 132
        });
147
148 132
        $this->bind('arcanedev.geoip', Contracts\GeoIP::class);
149 132
    }
150
}
151