Completed
Push — master ( 723848...f6a9f3 )
by Yaro
05:45
created

ServiceProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 79.69%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 9
dl 0
loc 131
ccs 51
cts 64
cp 0.7969
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 6 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\ViewComponents\Breadcrumbs\Breadcrumbs;
13
use Yaro\Jarboe\ViewComponents\Breadcrumbs\BreadcrumbsInterface;
14
15
class ServiceProvider extends IlluminateServiceProvider
16
{
17
18
    /**
19
     * A list of artisan commands
20
     *
21
     * @var array
22
     */
23
    protected $commands = [
24
        Install::class,
25
        MakeTool::class,
26
    ];
27
28
    /**
29
     * Bootstrap the application services.
30
     *
31
     * @return void
32
     */
33 834
    public function boot()
34
    {
35 834
        $this->publishes([
36 834
            __DIR__.'/../config/admin_panel.php' => config_path('jarboe/admin_panel.php'),
37 834
            __DIR__.'/../config/crud.php' => config_path('jarboe/crud.php'),
38 834
            __DIR__.'/../config/locales.php' => config_path('jarboe/locales.php'),
39 834
        ], 'config');
40
41 834
        if ($this->app->runningInConsole()) {
42 834
            $this->commands($this->commands);
43
        }
44
45
        //$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
46 834
        $this->loadRoutesFrom(__DIR__.'/../routes/common.php');
47 834
        $this->loadRoutesFrom(__DIR__.'/../routes/auth.php');
48 834
        if (config('jarboe.admin_panel.default_routes_enabled')) {
49 834
            $this->loadRoutesFrom(__DIR__.'/../routes/admins.php');
50
        }
51 834
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'jarboe');
52 834
        $this->publishes([
53 834
            __DIR__.'/../resources/assets' => public_path('vendor/jarboe'),
54 834
        ], 'public');
55
56 834
        $this->loadTranslationsFrom(__DIR__ . '/../resources/lang', 'jarboe');
57
58 834
        $this->registerViewComposer();
59 834
        $this->registerBladeDirectives();
60 834
        $this->registerMiddlewareGroup();
61 834
        $this->registerBindings();
62 834
    }
63
64
    /**
65
     * Register the application services.
66
     *
67
     * @return void
68
     */
69 834
    public function register()
70
    {
71 834
        $this->mergeConfigFrom(__DIR__.'/../config/admin_panel.php', 'jarboe.admin_panel');
72 834
        $this->mergeConfigFrom(__DIR__.'/../config/crud.php', 'jarboe.crud');
73 834
        $this->mergeConfigFrom(__DIR__.'/../config/locales.php', 'jarboe.locales');
74
75
        $this->app->singleton('jarboe', function ($app) {
76 834
            return new Jarboe();
77 834
        });
78
79 834
        $this->initFallbackRoute();
80 834
    }
81
82 834
    private function registerBladeDirectives()
83
    {
84
        Blade::directive('pushonce', function ($expression) {
85
            $params = collect(explode(',', $expression))->map(function ($item) {
86
                return trim($item);
87
            });
88
            $stack = $params->shift();
89
            $content = $params->implode(',');
90
91
            $isDisplayed = '__pushonce_'.md5($content);
92
            return "<?php if(!isset(\$__env->{$isDisplayed})): \$__env->{$isDisplayed} = true; \$__env->startPush({$stack}); ?>"
93
                . $content
94
                . '<?php $__env->stopPush(); endif; ?>';
95 834
        });
96 834
    }
97
98 834
    private function registerMiddlewareGroup()
99
    {
100 834
        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...
101 834
            $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...
102
        }
103 834
    }
104
105 834
    private function registerViewComposer()
106
    {
107
        View::composer('jarboe::layouts.main', function ($view) {
108
            $themes = [
109
                'default' => 'smart-style-0',
110
                'dark' => 'smart-style-1',
111
                'light' => 'smart-style-2',
112
                'google-skin' => 'smart-style-3',
113
                'pixel-smash' => 'smart-style-4',
114
                'glass' => 'smart-style-5',
115
                'material' => 'smart-style-6',
116
            ];
117
118
            $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...
119
            $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...
120 834
        });
121
122
        View::composer('jarboe::inc.locale_selector', function ($view) {
123
            $view->localeHelper = new Locale();
124 834
        });
125 834
    }
126
127 834
    private function initFallbackRoute()
128
    {
129
        $this->app->booted(function () {
130 834
            $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...
131
            $router->group(app('jarboe')->routeGroupOptions(), function () use ($router) {
132
                $router->get('{any}', function () {
133
                    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...
134 834
                })->where('any', '.*');
135 834
            });
136 834
        });
137 834
    }
138
139 834
    private function registerBindings()
140
    {
141
        $this->app->bind(BreadcrumbsInterface::class, function ($app) {
142 41
            return new Breadcrumbs();
143 834
        });
144 834
    }
145
}
146