Completed
Push — master ( ba702a...fd24a7 )
by Mostafa Abd El-Salam
10:50 queued 06:35
created

PermissionDirectives::hasallrolesDirective()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
crap 1
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 View Code Duplication
    public function hasroleDirective()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function hasanyroleDirective()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function hasallrolesDirective()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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