Completed
Branch master (49dc1a)
by Yaro
01:42
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\Foundation\Application;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Facades\Blade;
8
use Illuminate\Support\Facades\View;
9
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;
10
use Yaro\Jarboe\Console\Commands\Install;
11
use Yaro\Jarboe\Console\Commands\Make\Tool as MakeTool;
12
use Yaro\Jarboe\Helpers\Locale;
13
use Yaro\Jarboe\Helpers\System;
14
use Yaro\Jarboe\Table\Repositories\EloquentModelRepository;
15
use Yaro\Jarboe\Table\Repositories\ModelRepositoryInterface;
16
use Yaro\Jarboe\ViewComponents\Breadcrumbs\Breadcrumbs;
17
use Yaro\Jarboe\ViewComponents\Breadcrumbs\BreadcrumbsInterface;
18
19
class ServiceProvider extends IlluminateServiceProvider
20
{
21
    /**
22
     * A list of artisan commands
23
     *
24
     * @var array
25
     */
26
    protected $commands = [
27
        Install::class,
28
        MakeTool::class,
29
    ];
30
31
    /**
32
     * Bootstrap the application services.
33
     *
34
     * @return void
35
     */
36 1132
    public function boot()
37
    {
38 1132
        $this->publishes([
39 1132
            __DIR__.'/../config/admin_panel.php' => config_path('jarboe/admin_panel.php'),
40 1132
            __DIR__.'/../config/crud.php' => config_path('jarboe/crud.php'),
41 1132
            __DIR__.'/../config/locales.php' => config_path('jarboe/locales.php'),
42 1132
            __DIR__.'/../config/versionable.php' => config_path('jarboe/versionable.php'),
43 1132
        ], 'config');
44
45 1132
        if ($this->app->runningInConsole()) {
46 1132
            $this->commands($this->commands);
47
        }
48
49
        //$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
50 1132
        $this->loadRoutesFrom(__DIR__.'/../routes/common.php');
51 1132
        $this->loadRoutesFrom(__DIR__.'/../routes/auth.php');
52 1132
        if (config('jarboe.admin_panel.default_routes_enabled')) {
53 1132
            $this->loadRoutesFrom(__DIR__.'/../routes/admins.php');
54
        }
55 1132
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'jarboe');
56 1132
        $this->publishes([
57 1132
            __DIR__.'/../resources/assets' => public_path('vendor/jarboe'),
58 1132
        ], 'public');
59
60 1132
        $this->loadTranslationsFrom(__DIR__ . '/../resources/lang', 'jarboe');
61
62 1132
        $this->registerViewComposer();
63 1132
        $this->registerBladeDirectives();
64 1132
        $this->registerMiddlewareGroup();
65 1132
        $this->registerBindings();
66 1132
    }
67
68
    /**
69
     * Register the application services.
70
     *
71
     * @return void
72
     */
73 1132
    public function register()
74
    {
75 1132
        $this->mergeConfigFrom(__DIR__.'/../config/admin_panel.php', 'jarboe.admin_panel');
76 1132
        $this->mergeConfigFrom(__DIR__.'/../config/crud.php', 'jarboe.crud');
77 1132
        $this->mergeConfigFrom(__DIR__.'/../config/locales.php', 'jarboe.locales');
78 1132
        $this->mergeConfigFrom(__DIR__.'/../config/versionable.php', 'jarboe.versionable');
79
80
        $this->app->singleton('jarboe', function ($app) {
81 1132
            return new Jarboe();
82 1132
        });
83
84 1132
        $this->initFallbackRoute();
85 1132
    }
86
87 1132
    private function registerBladeDirectives()
88
    {
89
        Blade::directive('pushonce', function ($expression) {
90
            $params = collect(explode(',', $expression))->map(function ($item) {
91
                return trim($item);
92
            });
93
            $stack = $params->shift();
94
            $content = $params->implode(',');
95
96
            $isDisplayed = '__pushonce_'.md5($content);
97
            return "<?php if(!isset(\$__env->{$isDisplayed})): \$__env->{$isDisplayed} = true; \$__env->startPush({$stack}); ?>"
98
                . $content
99
                . '<?php $__env->stopPush(); endif; ?>';
100 1132
        });
101 1132
    }
102
103 1132
    private function registerMiddlewareGroup()
104
    {
105 1132
        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...
106 1132
            $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...
107
        }
108 1132
    }
109
110 1132
    private function registerViewComposer()
111
    {
112
        View::composer('jarboe::layouts.main', function ($view) {
113
            $themes = [
114
                'brown' => 'smart-style-0',
115
                'dark' => 'smart-style-1',
116
                'light' => 'smart-style-2',
117
                'google-skin' => 'smart-style-3',
118
                'pixel-smash' => 'smart-style-4',
119
                'glass' => 'smart-style-5',
120
                'material' => 'smart-style-6',
121
            ];
122
            $selectedTheme = $this->app->config->get('jarboe.admin_panel.theme', 'light');
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...
123
124
            $view->themeClass = Arr::get($themes, $selectedTheme, $selectedTheme);
125
            $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...
126
127
            $localeHelper = new Locale();
128
            $currentLocale = $localeHelper->current();
129
            if ($currentLocale == 'en' || $currentLocale == 'en-US') {
130
                $currentLocale = false;
131
            }
132
            $view->currentLocale = $currentLocale;
133 1132
        });
134
135
        View::composer('jarboe::inc.locale_selector', function ($view) {
136
            $view->localeHelper = new Locale();
137 1132
        });
138
139
        View::composer('jarboe::inc.footer', function ($view) {
140
            $view->jarboeVersion = Jarboe::VERSION;
141
            $view->laravelVersion = Application::VERSION;
142
            $view->system = new System();
143 1132
        });
144 1132
    }
145
146 1132
    private function initFallbackRoute()
147
    {
148
        $this->app->booted(function () {
149 1132
            $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...
150
            $router->group(app('jarboe')->routeGroupOptions(), function () use ($router) {
151
                $router->get('{any}', function () {
152
                    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...
153 1132
                })->where('any', '.*');
154 1132
            });
155 1132
        });
156 1132
    }
157
158 1132
    private function registerBindings()
159
    {
160
        $this->app->bind(BreadcrumbsInterface::class, function ($app) {
161 79
            return new Breadcrumbs();
162 1132
        });
163
        $this->app->bind(ModelRepositoryInterface::class, function ($app) {
164 115
            return new EloquentModelRepository();
165 1132
        });
166 1132
    }
167
}
168