Passed
Push — master ( 288c97...dba1b3 )
by Malte
02:54
created

LaravelServiceProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 3 1
A register() 0 10 1
B setVendorConfig() 0 31 8
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\IMAP\Client;
17
use Webklex\IMAP\ClientManager;
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->app->singleton(ClientManager::class, function($app) {
44
            return new ClientManager($app);
45
        });
46
47
        $this->app->singleton(Client::class, function($app) {
48
            return $app[ClientManager::class]->account();
49
        });
50
51
        $this->setVendorConfig();
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, array_merge($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
}