Test Failed
Pull Request — master (#39)
by Mostafa Abd El-Salam
02:34 queued 17s
created

PermissionDirectives::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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
    public function __construct(BladeCompiler $bladeCompiler)
16
    {
17
        $this->bladeCompiler = $bladeCompiler;
18
    }
19
20
    public function roleDirective()
21
    {
22
        $this->bladeCompiler->directive('role', function ($arguments) {
23
            list($role, $guard) = $this->extractRoleGuard($arguments);
24
25
            return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
26
        });
27
28
        $this->bladeCompiler->directive('endrole', function () {
29
            return '<?php endif; ?>';
30
        });
31
    }
32
33 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...
34
    {
35
        $this->bladeCompiler->directive('hasrole', function ($arguments) {
36
            list($role, $guard) = $this->extractRoleGuard($arguments);
37
38
            return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
39
        });
40
        $this->bladeCompiler->directive('endhasrole', function () {
41
            return '<?php endif; ?>';
42
        });
43
    }
44
45 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...
46
    {
47
        $this->bladeCompiler->directive('hasanyrole', function ($arguments) {
48
            list($roles, $guard) = $this->extractRoleGuard($arguments);
49
50
            return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAnyRole({$roles})): ?>";
51
        });
52
        $this->bladeCompiler->directive('endhasanyrole', function () {
53
            return '<?php endif; ?>';
54
        });
55
    }
56
57 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...
58
    {
59
        $this->bladeCompiler->directive('hasallroles', function ($arguments) {
60
            list($roles, $guard) = $this->extractRoleGuard($arguments);
61
62
            return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAllRoles({$roles})): ?>";
63
        });
64
        $this->bladeCompiler->directive('endhasallroles', function () {
65
            return '<?php endif; ?>';
66
        });
67
    }
68
69
    private function extractRoleGuard($arguments)
70
    {
71
        $arguments = preg_replace('(\(|\)| )', '', $arguments);
72
73
        return \explode(',', $arguments . ',');
74
    }
75
}
76