Completed
Push — master ( 4895be...67409c )
by Oleg
04:41
created

MenuServiceProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 9
Bugs 2 Features 0
Metric Value
wmc 9
c 9
b 2
f 0
lcom 1
cbo 4
dl 0
loc 96
ccs 42
cts 42
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 9 1
A register() 0 10 1
A registerSingleton() 0 7 1
A registerBuilder() 0 5 1
A registerAttributes() 0 5 1
A registerRenderSystem() 0 15 2
A registerComparativeUrl() 0 9 1
A provides() 0 4 1
1
<?php
2
3
namespace Malezha\Menu;
4
5
use Illuminate\Contracts\Config\Repository;
6
use Illuminate\Contracts\Container\Container;
7
use Illuminate\Support\ServiceProvider;
8
use Malezha\Menu\Contracts\Attributes as AttributesContract;
9
use Malezha\Menu\Contracts\Builder as BuilderContract;
10
use Malezha\Menu\Contracts\ComparativeUrl as ComparativeUrlContract;
11
use Malezha\Menu\Contracts\Menu as MenuContract;
12
use Malezha\Menu\Contracts\MenuRender;
13
use Malezha\Menu\Support\Attributes;
14
use Malezha\Menu\Support\ComparativeUrl;
15
16
/**
17
 * Class MenuServiceProvider
18
 * @package Malezha\Menu
19
 */
20
class MenuServiceProvider extends ServiceProvider
21
{
22
    /**
23
     * Indicates if loading of the provider is deferred.
24
     *
25
     * @var bool
26
     */
27
    protected $defer = false;
28
29
    /**
30
     * Bootstrap the application services.
31
     *
32
     * @return void
33
     */
34 54
    public function boot()
35
    {
36 54
        $this->loadViewsFrom(__DIR__ . '/../views', 'menu');
37
38 54
        $this->publishes([
39 54
            __DIR__ . '/../views' => base_path('resources/views/vendor/menu'),
40 54
            __DIR__ . '/../config/menu.php' => config_path('menu.php'),
41
        ]);
42 54
    }
43
44
    /**
45
     * Register the application services.
46
     *
47
     * @return void
48
     */
49 54
    public function register()
50
    {
51 54
        $this->mergeConfigFrom(__DIR__ . '/../config/menu.php', 'menu');
52
53 54
        $this->registerComparativeUrl();
54 54
        $this->registerRenderSystem();
55 54
        $this->registerAttributes();
56 54
        $this->registerBuilder();
57 54
        $this->registerSingleton();
58 54
    }
59
60 54
    protected function registerSingleton()
61
    {
62
        $this->app->singleton('menu.instance', function(Container $app) {
63 7
            return new Menu($app);
64 54
        });
65 54
        $this->app->alias('menu.instance', MenuContract::class);
66 54
    }
67
68 54
    protected function registerBuilder()
69
    {
70 54
        $this->app->bind('menu.builder', Builder::class);
71 54
        $this->app->alias('menu.builder', BuilderContract::class);
72 54
    }
73
74 54
    protected function registerAttributes()
75
    {
76 54
        $this->app->bind('menu.attributes', Attributes::class);
77 54
        $this->app->alias('menu.attributes', AttributesContract::class);
78 54
    }
79
80 54
    protected function registerRenderSystem()
81
    {
82
        $this->app->bind('menu.render', function (Container $app) {
83 33
            $config = $app->make(Repository::class)->get('menu');
84 33
            $key = $config['default'];
85 33
            $available = $config['renders'];
86
87 33
            if (array_key_exists($key, $available)) {
88 33
                return new $available[$key]($app);
89
            }
90
91
            throw new \Exception('Can use template system: ' . $config['default']);
92 54
        });
93 54
        $this->app->alias('menu.render', MenuRender::class);
94 54
    }
95
96
    protected function registerComparativeUrl()
97
    {
98 54
        $this->app->singleton('menu.compare-url', function (Container $app) {
99 24
            return $app->make(ComparativeUrl::class, [
100 24
                'skippedPaths' => $app->make(Repository::class)->get('menu.skippedPaths'),
101
            ]);
102 54
        });
103 54
        $this->app->alias('menu.compare-url', ComparativeUrlContract::class);
104 54
    }
105
106
    /**
107
     * Get the services provided by the provider.
108
     *
109
     * @return array
110
     */
111
    public function provides()
112
    {
113
        return [];
114
    }
115
}
116