PermissionDirectives::hasanyroleDirective()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Maklad\Permission\Directives;
4
5
use Illuminate\View\Compilers\BladeCompiler;
6
7
/**
8
 * Class PermissionDirectives
9
 * @package Maklad\Permission\Directives
10
 */
11
class PermissionDirectives
12
{
13
    private $bladeCompiler;
14
15 18
    public function __construct(BladeCompiler $bladeCompiler)
16
    {
17 18
        $this->bladeCompiler = $bladeCompiler;
18 18
    }
19
20
    /**
21
     * Declare role directive
22
     */
23
    public function roleDirective()
24
    {
25 18
        $this->bladeCompiler->directive('role', function ($arguments) {
26 5
            list($role, $guard) = $this->extractRoleGuard($arguments);
27
28 5
            return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
29 18
        });
30
31 18
        $this->bladeCompiler->directive('endrole', function () {
32 5
            return '<?php endif; ?>';
33 18
        });
34 18
    }
35
36
    /**
37
     * Declare hasrole directive
38
     */
39
    public function hasroleDirective()
40
    {
41 18
        $this->bladeCompiler->directive('hasrole', function ($arguments) {
42 5
            list($role, $guard) = $this->extractRoleGuard($arguments);
43
44 5
            return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
45 18
        });
46 18
        $this->bladeCompiler->directive('endhasrole', function () {
47 5
            return '<?php endif; ?>';
48 18
        });
49 18
    }
50
51
    /**
52
     * Declare hasanyrole directive
53
     */
54
    public function hasanyroleDirective()
55
    {
56 18
        $this->bladeCompiler->directive('hasanyrole', function ($arguments) {
57 8
            list($roles, $guard) = $this->extractRoleGuard($arguments);
58
59 8
            return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAnyRole({$roles})): ?>";
60 18
        });
61 18
        $this->bladeCompiler->directive('endhasanyrole', function () {
62 8
            return '<?php endif; ?>';
63 18
        });
64 18
    }
65
66
    /**
67
     * Declare hasallroles directive
68
     */
69
    public function hasallrolesDirective()
70
    {
71 18
        $this->bladeCompiler->directive('hasallroles', function ($arguments) {
72 8
            list($roles, $guard) = $this->extractRoleGuard($arguments);
73
74 8
            return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAllRoles({$roles})): ?>";
75 18
        });
76 18
        $this->bladeCompiler->directive('endhasallroles', function () {
77 8
            return '<?php endif; ?>';
78 18
        });
79 18
    }
80
81
    /**
82
     * @param $arguments
83
     *
84
     * @return array
85
     */
86 17
    private function extractRoleGuard($arguments): array
87
    {
88 17
        $arguments = preg_replace('(\(|\)| )', '', $arguments);
89
90 17
        return \explode(',', $arguments . ',');
91
    }
92
}
93