Completed
Push — master ( 798e92...ea2dad )
by Yaro
04:12 queued 26s
created

ServiceProvider::boot()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 22
cts 22
cp 1
rs 9.44
c 0
b 0
f 0
cc 3
nc 4
nop 0
crap 3
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 873
    public function boot()
36
    {
37 873
        $this->publishes([
38 873
            __DIR__.'/../config/admin_panel.php' => config_path('jarboe/admin_panel.php'),
39 873
            __DIR__.'/../config/crud.php' => config_path('jarboe/crud.php'),
40 873
            __DIR__.'/../config/locales.php' => config_path('jarboe/locales.php'),
41 873
        ], 'config');
42
43 873
        if ($this->app->runningInConsole()) {
44 873
            $this->commands($this->commands);
45
        }
46
47
        //$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
48 873
        $this->loadRoutesFrom(__DIR__.'/../routes/common.php');
49 873
        $this->loadRoutesFrom(__DIR__.'/../routes/auth.php');
50 873
        if (config('jarboe.admin_panel.default_routes_enabled')) {
51 873
            $this->loadRoutesFrom(__DIR__.'/../routes/admins.php');
52
        }
53 873
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'jarboe');
54 873
        $this->publishes([
55 873
            __DIR__.'/../resources/assets' => public_path('vendor/jarboe'),
56 873
        ], 'public');
57
58 873
        $this->loadTranslationsFrom(__DIR__ . '/../resources/lang', 'jarboe');
59
60 873
        $this->registerViewComposer();
61 873
        $this->registerBladeDirectives();
62 873
        $this->registerMiddlewareGroup();
63 873
        $this->registerBindings();
64 873
    }
65
66
    /**
67
     * Register the application services.
68
     *
69
     * @return void
70
     */
71 873
    public function register()
72
    {
73 873
        $this->mergeConfigFrom(__DIR__.'/../config/admin_panel.php', 'jarboe.admin_panel');
74 873
        $this->mergeConfigFrom(__DIR__.'/../config/crud.php', 'jarboe.crud');
75 873
        $this->mergeConfigFrom(__DIR__.'/../config/locales.php', 'jarboe.locales');
76
77
        $this->app->singleton('jarboe', function ($app) {
78 873
            return new Jarboe();
79 873
        });
80
81 873
        $this->initFallbackRoute();
82 873
    }
83
84 873
    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 873
        });
98 873
    }
99
100 873
    private function registerMiddlewareGroup()
101
    {
102 873
        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 873
            $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 873
    }
106
107 873
    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 873
        });
123
124
        View::composer('jarboe::inc.locale_selector', function ($view) {
125
            $view->localeHelper = new Locale();
126 873
        });
127 873
    }
128
129 873
    private function initFallbackRoute()
130
    {
131
        $this->app->booted(function () {
132 873
            $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 873
                })->where('any', '.*');
137 873
            });
138 873
        });
139 873
    }
140
141 873
    private function registerBindings()
142
    {
143
        $this->app->bind(BreadcrumbsInterface::class, function ($app) {
144 70
            return new Breadcrumbs();
145 873
        });
146
        $this->app->bind(ModelRepositoryInterface::class, function ($app) {
147 88
            return new EloquentModelRepository();
148 873
        });
149 873
    }
150
}
151