PermissionServiceProvider::boot()   A
last analyzed

Complexity

Conditions 5
Paths 18

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5

Importance

Changes 9
Bugs 1 Features 2
Metric Value
cc 5
eloc 20
c 9
b 1
f 2
nc 18
nop 0
dl 0
loc 30
ccs 17
cts 17
cp 1
crap 5
rs 9.2888
1
<?php
2
3
namespace Maklad\Permission;
4
5
use Illuminate\Support\ServiceProvider;
6
use Illuminate\View\Compilers\BladeCompiler;
7
use Maklad\Permission\Contracts\PermissionInterface as Permission;
8
use Maklad\Permission\Contracts\RoleInterface as Role;
9
use Maklad\Permission\Directives\PermissionDirectives;
10
use Illuminate\Support\Facades\DB;
11
12
/**
13
 * Class PermissionServiceProvider
14
 * @package Maklad\Permission
15
 */
16
class PermissionServiceProvider extends ServiceProvider
17 123
{
18
    public function boot()
19 123
    {
20 123
        $helpers = new Helpers();
21 123
        if ($helpers->isNotLumen()) {
22 123
            $this->publishes([
23 123
                __DIR__ . '/../config/permission.php' => $this->app->configPath() . '/permission.php',
24
            ], 'config');
25
26 123
            if (!class_exists('CreatePermissionTables')) {
27 123
                $timestamp = date('Y_m_d_His');
28 123
                $mFilePath = $this->app->databasePath() . "/migrations/{$timestamp}_create_permission_collections.php";
29
                $this->publishes([
30
                    __DIR__ . '/../database/migrations/create_permission_collections.php.stub' => $mFilePath,
31
                ], 'migrations');
32
            }
33 123
        }
34
35 123
        if ($this->app->runningInConsole()) {
36 123
            $this->commands([
37
                Commands\CreateRole::class,
38 123
                Commands\CreatePermission::class,
39
            ]);
40 123
        }
41 123
42 123
        $this->registerModelBindings();
43 123
44 123
        try {
45
            DB::connection()->getPdo();
46
            app(PermissionRegistrar::class)->registerPermissions();
47
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
48 123
        }
49 123
    }
50
51 123
    public function register()
52
    {
53 123
        $helpers = new Helpers();
54
        if ($helpers->isNotLumen()) {
55 123
            $this->mergeConfigFrom(
56 123
                __DIR__ . '/../config/permission.php',
57 123
                'permission'
58
            );
59
        }
60
61 123
        $this->registerBladeExtensions();
62 18
    }
63
64 18
    protected function registerModelBindings()
65 18
    {
66 18
        $config = $this->app->config['permission.models'];
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?
Loading history...
67 18
68 123
        $this->app->bind(Permission::class, $config['permission']);
69 123
        $this->app->bind(Role::class, $config['role']);
70
    }
71
72
    protected function registerBladeExtensions()
73
    {
74
        $this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
75
            $permissionDirectives = new PermissionDirectives($bladeCompiler);
76
77
            $permissionDirectives->roleDirective();
78
            $permissionDirectives->hasroleDirective();
79
            $permissionDirectives->hasanyroleDirective();
80
            $permissionDirectives->hasallrolesDirective();
81
        });
82
    }
83
}
84