LaravelServiceProvider   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 22
eloc 40
c 2
b 1
f 0
dl 0
loc 119
rs 10

4 Methods

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