Completed
Push — master ( 814073...4a1646 )
by Yaro
10:23 queued 02:16
created

ServiceProvider::initFallbackRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1.0028

Importance

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