Completed
Push — master ( 34493c...af9c29 )
by Arjay
01:19
created

HasAuthorizations::makeIfCannot()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 4
nop 3
1
<?php
2
3
namespace Yajra\DataTables\Html;
4
5
use Illuminate\Contracts\Auth\Access\Authorizable;
6
7
trait HasAuthorizations
8
{
9
    /**
10
     * Flag to check if user is authorized to use the button.
11
     *
12
     * @var bool
13
     */
14
    protected $authorized = true;
15
16
    /**
17
     * Make a button if condition is true.
18
     *
19
     * @param bool|callable $condition
20
     * @param string|array $options
21
     * @return static
22
     */
23
    public static function makeIf($condition, $options)
24
    {
25
        if (value($condition)) {
26
            return static::make($options);
27
        }
28
29
        return static::make([])->authorized(false);
30
    }
31
32
    /**
33
     * Make a button if the user is authorized.
34
     *
35
     * @param string $permission
36
     * @param string|array $options
37
     * @param Authorizable|null $user
38
     * @return static
39
     */
40 View Code Duplication
    public static function makeIfCan($permission, $options, Authorizable $user = null)
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...
41
    {
42
        if (is_null($user)) {
43
            $user = auth()->user();
44
        }
45
46
        if ($user->can($permission)) {
47
            return static::make($options);
48
        }
49
50
        return static::make([])->authorized(false);
51
    }
52
53
    /**
54
     * Make a button if the user is not authorized.
55
     *
56
     * @param string $permission
57
     * @param string|array $options
58
     * @param Authorizable|null $user
59
     * @return static
60
     */
61 View Code Duplication
    public static function makeIfCannot($permission, $options, Authorizable $user = null)
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...
62
    {
63
        if (is_null($user)) {
64
            $user = auth()->user();
65
        }
66
67
        if (! $user->can($permission)) {
68
            return static::make($options);
69
        }
70
71
        return static::make([])->authorized(false);
72
    }
73
74
    /**
75
     * Set authorization status of the button.
76
     *
77
     * @param bool|callable $bool
78
     * @return static
79
     */
80
    public function authorized($bool)
81
    {
82
        $this->authorized = value($bool);
83
84
        return $this;
85
    }
86
87
    /**
88
     * Convert the Fluent instance to an array.
89
     *
90
     * @return array
91
     */
92
    public function toArray()
93
    {
94
        if ($this->authorized) {
95
            return parent::toArray();
96
        }
97
98
        return [];
99
    }
100
}
101