Completed
Push — master ( 5a9d2e...4895be )
by Oleg
04:31
created

MenuServiceProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 92.31%

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 36
cts 39
cp 0.9231
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 9 1
A registerSingleton() 0 7 1
A registerBuilder() 0 5 1
A registerAttributes() 0 5 1
A boot() 0 9 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 51
    public function boot()
33
    {
34 51
        $this->loadViewsFrom(__DIR__ . '/../views', 'menu');
35
36 51
        $this->publishes([
37 51
            __DIR__ . '/../views' => base_path('resources/views/vendor/menu'),
38 51
            __DIR__ . '/../config/menu.php' => config_path('menu.php'),
39 51
        ]);
40 51
    }
41
42
    /**
43
     * Register the application services.
44
     *
45
     * @return void
46
     */
47 51
    public function register()
48
    {
49 51
        $this->mergeConfigFrom(__DIR__ . '/../config/menu.php', 'menu');
50
        
51 51
        $this->registerRenderSystem();
52 51
        $this->registerAttributes();
53 51
        $this->registerBuilder();
54 51
        $this->registerSingleton();
55 51
    }
56
57 51
    protected function registerSingleton()
58
    {
59
        $this->app->singleton('menu.instance', function(Container $app) {
60 7
            return new Menu($app);
61 51
        });
62 51
        $this->app->alias('menu.instance', MenuContract::class);
63 51
    }
64
65 51
    protected function registerBuilder()
66
    {
67 51
        $this->app->bind('menu.builder', Builder::class);
68 51
        $this->app->alias('menu.builder', BuilderContract::class);
69 51
    }
70
71 51
    protected function registerAttributes()
72
    {
73 51
        $this->app->bind('menu.attributes', Attributes::class);
74 51
        $this->app->alias('menu.attributes', AttributesContract::class);
75 51
    }
76
77
    protected function registerRenderSystem()
78
    {
79 51
        $this->app->bind('menu.render', function (Container $app) {
80 33
            $config = $app->make(Repository::class)->get('menu');
81 33
            $key = $config['default'];
82 33
            $available = $config['renders'];
83
84 33
            if (array_key_exists($key, $available)) {
85 33
                return new $available[$key]($app);
86
            }
87
88
            throw new \Exception('Can use template system: ' . $config['default']);
89 51
        });
90 51
        $this->app->alias('menu.render', MenuRender::class);
91 51
    }
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