Completed
Push — master ( 26578f...8b8176 )
by wen
02:36
created

ComponentServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 34
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 8 1
A register() 0 4 1
A bindRouteModel() 0 9 2
A registerComponents() 0 7 2
1
<?php
2
3
namespace Sco\Admin\Providers;
4
5
use Illuminate\Database\Eloquent\ModelNotFoundException;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\ServiceProvider;
8
use Sco\Admin\Component\Component;
9
10
class ComponentServiceProvider extends ServiceProvider
11
{
12
    public function boot()
13
    {
14
        Component::setEventDispatcher($this->app['events']);
15
16
        $this->registerComponents();
17
18
        $this->bindRouteModel();
19
    }
20
21
    public function register()
22
    {
23
        $this->app->instance('admin.components', new Collection());
24
    }
25
26
    protected function bindRouteModel()
27
    {
28
        $this->app['router']->bind('model', function ($value, $route) {
0 ignored issues
show
Unused Code introduced by
The parameter $route is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
29
            if (!$this->app['admin.components']->has($value)) {
30
                throw new ModelNotFoundException();
31
            }
32
            return $this->app['admin.components']->get($value);
33
        });
34
    }
35
36
    protected function registerComponents()
37
    {
38
        foreach (config('admin.components', []) as $model => $component) {
39
            $class = new $component($this->app, $model);
40
            $this->app['admin.components']->put($class->getName(), $class);
41
        }
42
    }
43
}
44