Completed
Push — master ( e84808...5a9d2e )
by Oleg
07:58
created

MenuServiceProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 2 Features 0
Metric Value
wmc 8
c 8
b 2
f 0
lcom 1
cbo 4
dl 0
loc 85
ccs 35
cts 35
cp 1
rs 10

7 Methods

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