GeoIPServiceProvider   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 27
c 1
b 0
f 0
dl 0
loc 92
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 5 1
A boot() 0 3 1
C array_merge_recursive_distinct() 0 31 12
A setVendorConfig() 0 8 1
1
<?php
2
/*
3
* File: GeoIPServiceProvider.php
4
* Category: ServiceProvider
5
* Author: M.Goldenbaum
6
* Created: 18.10.20 00:42
7
* Updated: -
8
*
9
* Description:
10
*  -
11
*/
12
13
namespace Webklex\LaravelGeoIP\Providers;
14
15
use Illuminate\Support\ServiceProvider;
16
use Webklex\GeoIP\GeoIP;
17
18
/**
19
 * Class GeoIPServiceProvider
20
 *
21
 * @package Webklex\IMAP\Providers
22
 */
23
class GeoIPServiceProvider extends ServiceProvider {
24
25
    /**
26
     * Perform post-registration booting of services.
27
     *
28
     * @return void
29
     */
30
    public function boot() {
31
        $this->publishes([
32
            __DIR__ . '/../config/geoip.php' => config_path('geoip.php'),
33
        ]);
34
    }
35
36
    /**
37
     * Register any package services.
38
     *
39
     * @return void
40
     */
41
    public function register() {
42
        $this->setVendorConfig();
43
44
        $this->app->singleton(GeoIP::class, function($app) {
45
            return new GeoIP($app['config']["geoip"]["endpoint"]);
46
        });
47
    }
48
49
    /**
50
     * Merge the vendor settings with the local config
51
     *
52
     * The default account identifier will be used as default for any missing account parameters.
53
     * If however the default account is missing a parameter the package default account parameter will be used.
54
     * This can be disabled by setting imap.default in your config file to 'false'
55
     */
56
    private function setVendorConfig() {
57
        $config_key = 'geoip';
58
        $path = __DIR__ . '/../config/' . $config_key . '.php';
59
60
        $vendor_config = require $path;
61
        $config = $this->app['config']->get($config_key);
62
63
        $this->app['config']->set($config_key, $this->array_merge_recursive_distinct($vendor_config, $config));
64
    }
65
66
    /**
67
     * Marge arrays recursively and distinct
68
     *
69
     * Merges any number of arrays / parameters recursively, replacing
70
     * entries with string keys with values from latter arrays.
71
     * If the entry or the next value to be assigned is an array, then it
72
     * automatically treats both arguments as an array.
73
     * Numeric entries are appended, not replaced, but only if they are
74
     * unique
75
     *
76
     * @param array $array1 Initial array to merge.
77
     * @param array ...     Variable list of arrays to recursively merge.
78
     *
79
     * @return array|mixed
80
     *
81
     * @link   http://www.php.net/manual/en/function.array-merge-recursive.php#96201
82
     * @author Mark Roduner <[email protected]>
83
     */
84
    private function array_merge_recursive_distinct() {
85
86
        $arrays = func_get_args();
87
        $base = array_shift($arrays);
88
89
        if (!is_array($base)) $base = empty($base) ? array() : array($base);
90
91
        foreach ($arrays as $append) {
92
93
            if (!is_array($append)) $append = array($append);
94
95
            foreach ($append as $key => $value) {
96
97
                if (!array_key_exists($key, $base) and !is_numeric($key)) {
98
                    $base[$key] = $append[$key];
99
                    continue;
100
                }
101
102
                if (is_array($value) or is_array($base[$key])) {
103
                    $base[$key] = $this->array_merge_recursive_distinct($base[$key], $append[$key]);
104
                } else if (is_numeric($key)) {
105
                    if (!in_array($value, $base)) $base[] = $value;
106
                } else {
107
                    $base[$key] = $value;
108
                }
109
110
            }
111
112
        }
113
114
        return $base;
115
    }
116
117
}