AppsServiceProvider   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 106
ccs 41
cts 41
cp 1
rs 10
c 2
b 0
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setupAssets() 0 3 1
A register() 0 9 1
A registerAppManager() 0 7 1
A setupConfiguration() 0 13 4
A boot() 0 6 2
A registerConfiguredProviders() 0 12 3
A publishAssets() 0 5 1
1
<?php
2
3
namespace ElfSundae\Apps;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\ServiceProvider;
7
8
class AppsServiceProvider extends ServiceProvider
9
{
10
    /**
11
     * Bootstrap the service provider.
12
     *
13
     * @return void
14
     */
15 24
    public function boot()
16
    {
17 24
        (new MacroRegistrar)->registerMacros($this->app);
18
19 24
        if ($this->app->runningInConsole()) {
20 24
            $this->publishAssets();
21
        }
22 18
    }
23
24
    /**
25
     * Publish assets from package.
26
     *
27
     * @return void
28
     */
29 24
    protected function publishAssets()
30
    {
31 24
        $this->publishes([
32 24
            __DIR__.'/../config/apps.php' => config_path('apps.php'),
33 24
        ], 'laravel-apps');
34 18
    }
35
36
    /**
37
     * Register the service provider.
38
     *
39
     * @return void
40
     */
41 32
    public function register()
42
    {
43 32
        $this->setupAssets();
44
45 32
        $this->registerAppManager();
46
47 32
        $this->registerConfiguredProviders();
48
49 32
        $this->setupConfiguration();
50 24
    }
51
52
    /**
53
     * Setup package assets.
54
     *
55
     * @return void
56
     */
57 32
    protected function setupAssets()
58
    {
59 32
        $this->mergeConfigFrom(__DIR__.'/../config/apps.php', 'apps');
60 24
    }
61
62
    /**
63
     * Register app manager singleton.
64
     *
65
     * @return void
66
     */
67 32
    protected function registerAppManager()
68
    {
69 8
        $this->app->singleton('apps', function ($app) {
70 32
            return new AppManager($app);
71 24
        });
72
73 32
        $this->app->alias('apps', AppManager::class);
74 24
    }
75
76
    /**
77
     * Register the configured service providers.
78
     *
79
     * @return void
80
     */
81 32
    protected function registerConfiguredProviders()
82
    {
83 32
        $providers = $this->app['config']->get('apps.providers', []);
84
85 32
        if ($this->app->runningInConsole()) {
86 24
            $providers = array_unique(Arr::flatten($providers));
87
        } else {
88 8
            $providers = (array) Arr::get($providers, $this->app['apps']->id());
89
        }
90
91 32
        foreach ($providers as $p) {
92 8
            $this->app->register($p);
93
        }
94 24
    }
95
96
    /**
97
     * Setup application configurations.
98
     *
99
     * @return void
100
     */
101 32
    protected function setupConfiguration()
102
    {
103 32
        $booting = $this->app->isBooted() ? 'booted' : 'booting';
104
105 8
        $this->app->$booting(function ($app) {
106 24
            $config = $app['config'];
107
108 24
            if (! $app->configurationIsCached()) {
109 24
                $config->set($config->get('apps.config.default', []));
110
            }
111
112 24
            if ($appId = $app['apps']->id()) {
113 20
                $config->set($config->get('apps.config.'.$appId, []));
114
            }
115 24
        });
116 24
    }
117
}
118