Completed
Pull Request — master (#10)
by ARCANEDEV
05:17 queued 03:44
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 44
    public function register()
42
    {
43 44
        parent::register();
44
45 44
        $this->registerConfig();
46
47 44
        $this->registerGeoIpManager();
48 44
        $this->registerGeoIpCache();
49 44
        $this->registerGeoIp();
50
51 44
        $this->registerConsoleServiceProvider(Providers\CommandServiceProvider::class);
52 44
    }
53
54
    /**
55
     * Boot the service provider.
56
     */
57 44
    public function boot()
58
    {
59 44
        parent::boot();
60
61 44
        $this->publishConfig();
62 44
    }
63
64
    /**
65
     * Get the services provided by the provider.
66
     *
67
     * @return array
68
     */
69 4
    public function provides()
70
    {
71
        return [
72 4
            Contracts\GeoIP::class,
73
            Contracts\DriverFactory::class,
74
            Contracts\GeoIPDriver::class,
75
            Contracts\GeoIPCache::class,
76
            Contracts\DriverFactory::class,
77
        ];
78
    }
79
80
    /* -----------------------------------------------------------------
81
     |  Other Methods
82
     | -----------------------------------------------------------------
83
     */
84
85
    /**
86
     * Register the GeoIP manager.
87
     */
88
    private function registerGeoIpManager()
89
    {
90 44
        $this->singleton(Contracts\DriverFactory::class, function ($app) {
91 32
            return new DriverManager($app);
92 44
        });
93
94 44
        $this->singleton(Contracts\GeoIPDriver::class, function ($app) {
95
            /** @var  \Arcanedev\GeoIP\Contracts\DriverFactory  $manager */
96 12
            $manager = $app[Contracts\DriverFactory::class];
97
98 12
            return $manager->driver();
99 44
        });
100 44
    }
101
102
    /**
103
     * Register the GeoIP Cache store.
104
     */
105
    private function registerGeoIpCache()
106
    {
107 44
        $this->singleton(Contracts\GeoIPCache::class, function ($app) {
108
            /** @var \Illuminate\Contracts\Config\Repository $config */
109 12
            $config = $app['config'];
110
111 12
            return new Cache(
112 12
                $app['cache.store'],
113 12
                $config->get('geoip.cache.tags', []),
114 12
                $config->get('geoip.cache.expires', 30)
115
            );
116 44
        });
117 44
    }
118
119
    /**
120
     * Register the GeoIP.
121
     */
122
    private function registerGeoIp()
123
    {
124 44
        $this->singleton(Contracts\GeoIP::class, function ($app) {
125
            /** @var \Illuminate\Contracts\Config\Repository $config */
126 12
            $config = $app['config'];
127
128 12
            return new GeoIP(
129 12
                $app[Contracts\GeoIPDriver::class],
130 12
                $app[Contracts\GeoIPCache::class],
131 12
                Arr::only($config->get('geoip', []), ['cache', 'location', 'currencies'])
132
            );
133 44
        });
134
    }
135
}
136