Completed
Push — master ( 956bd9...5c4b87 )
by Yaro
06:08
created

ServiceProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 80.3%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 10
dl 0
loc 134
ccs 53
cts 66
cp 0.803
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 30 3
A register() 0 12 1
A registerBladeDirectives() 0 15 1
A registerMiddlewareGroup() 0 6 2
A registerViewComposer() 0 21 1
A initFallbackRoute() 0 11 1
A registerBindings() 0 9 1
1
<?php
2
3
namespace Yaro\Jarboe;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Facades\Blade;
7
use Illuminate\Support\Facades\View;
8
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;
9
use Yaro\Jarboe\Console\Commands\Install;
10
use Yaro\Jarboe\Console\Commands\Make\Tool as MakeTool;
11
use Yaro\Jarboe\Helpers\Locale;
12
use Yaro\Jarboe\Table\Repositories\EloquentModelRepository;
13
use Yaro\Jarboe\Table\Repositories\ModelRepositoryInterface;
14
use Yaro\Jarboe\ViewComponents\Breadcrumbs\Breadcrumbs;
15
use Yaro\Jarboe\ViewComponents\Breadcrumbs\BreadcrumbsInterface;
16
17
class ServiceProvider extends IlluminateServiceProvider
18
{
19
20
    /**
21
     * A list of artisan commands
22
     *
23
     * @var array
24
     */
25
    protected $commands = [
26
        Install::class,
27
        MakeTool::class,
28
    ];
29
30
    /**
31
     * Bootstrap the application services.
32
     *
33
     * @return void
34
     */
35 856
    public function boot()
36
    {
37 856
        $this->publishes([
38 856
            __DIR__.'/../config/admin_panel.php' => config_path('jarboe/admin_panel.php'),
39 856
            __DIR__.'/../config/crud.php' => config_path('jarboe/crud.php'),
40 856
            __DIR__.'/../config/locales.php' => config_path('jarboe/locales.php'),
41 856
        ], 'config');
42
43 856
        if ($this->app->runningInConsole()) {
44 856
            $this->commands($this->commands);
45
        }
46
47
        //$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
48 856
        $this->loadRoutesFrom(__DIR__.'/../routes/common.php');
49 856
        $this->loadRoutesFrom(__DIR__.'/../routes/auth.php');
50 856
        if (config('jarboe.admin_panel.default_routes_enabled')) {
51 856
            $this->loadRoutesFrom(__DIR__.'/../routes/admins.php');
52
        }
53 856
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'jarboe');
54 856
        $this->publishes([
55 856
            __DIR__.'/../resources/assets' => public_path('vendor/jarboe'),
56 856
        ], 'public');
57
58 856
        $this->loadTranslationsFrom(__DIR__ . '/../resources/lang', 'jarboe');
59
60 856
        $this->registerViewComposer();
61 856
        $this->registerBladeDirectives();
62 856
        $this->registerMiddlewareGroup();
63 856
        $this->registerBindings();
64 856
    }
65
66
    /**
67
     * Register the application services.
68
     *
69
     * @return void
70
     */
71 856
    public function register()
72
    {
73 856
        $this->mergeConfigFrom(__DIR__.'/../config/admin_panel.php', 'jarboe.admin_panel');
74 856
        $this->mergeConfigFrom(__DIR__.'/../config/crud.php', 'jarboe.crud');
75 856
        $this->mergeConfigFrom(__DIR__.'/../config/locales.php', 'jarboe.locales');
76
77
        $this->app->singleton('jarboe', function ($app) {
78 856
            return new Jarboe();
79 856
        });
80
81 856
        $this->initFallbackRoute();
82 856
    }
83
84 856
    private function registerBladeDirectives()
85
    {
86
        Blade::directive('pushonce', function ($expression) {
87
            $params = collect(explode(',', $expression))->map(function ($item) {
88
                return trim($item);
89
            });
90
            $stack = $params->shift();
91
            $content = $params->implode(',');
92
93
            $isDisplayed = '__pushonce_'.md5($content);
94
            return "<?php if(!isset(\$__env->{$isDisplayed})): \$__env->{$isDisplayed} = true; \$__env->startPush({$stack}); ?>"
95
                . $content
96
                . '<?php $__env->stopPush(); endif; ?>';
97 856
        });
98 856
    }
99
100 856
    private function registerMiddlewareGroup()
101
    {
102 856
        foreach ($this->app->config->get('jarboe.admin_panel.middleware_groups', []) as $group => $middlewares) {
0 ignored issues
show
Bug introduced by
Accessing config on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
103 856
            $this->app->router->middlewareGroup($group, $middlewares);
0 ignored issues
show
Bug introduced by
Accessing router on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
104
        }
105 856
    }
106
107 856
    private function registerViewComposer()
108
    {
109
        View::composer('jarboe::layouts.main', function ($view) {
110
            $themes = [
111
                'default' => 'smart-style-0',
112
                'dark' => 'smart-style-1',
113
                'light' => 'smart-style-2',
114
                'google-skin' => 'smart-style-3',
115
                'pixel-smash' => 'smart-style-4',
116
                'glass' => 'smart-style-5',
117
                'material' => 'smart-style-6',
118
            ];
119
120
            $view->themeClass = Arr::get($themes, $this->app->config->get('jarboe.admin_panel.theme', 'default'));
0 ignored issues
show
Bug introduced by
Accessing config on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
121
            $view->menuOnTop = $this->app->config->get('jarboe.admin_panel.menu_on_top');
0 ignored issues
show
Bug introduced by
Accessing config on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
122 856
        });
123
124
        View::composer('jarboe::inc.locale_selector', function ($view) {
125
            $view->localeHelper = new Locale();
126 856
        });
127 856
    }
128
129 856
    private function initFallbackRoute()
130
    {
131
        $this->app->booted(function () {
132 856
            $router = $this->app->router;
0 ignored issues
show
Bug introduced by
Accessing router on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
133
            $router->group(app('jarboe')->routeGroupOptions(), function () use ($router) {
134
                $router->get('{any}', function () {
135
                    return response()->view('jarboe::errors.404')->setStatusCode(404);
0 ignored issues
show
Bug introduced by
The method view does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
136 856
                })->where('any', '.*');
137 856
            });
138 856
        });
139 856
    }
140
141 856
    private function registerBindings()
142
    {
143
        $this->app->bind(BreadcrumbsInterface::class, function ($app) {
144 53
            return new Breadcrumbs();
145 856
        });
146
        $this->app->bind(ModelRepositoryInterface::class, function ($app) {
147 71
            return new EloquentModelRepository();
148 856
        });
149 856
    }
150
}
151