Completed
Push — master ( 3c2674...5698f6 )
by Ricardo
06:00
created

Access::denyAccess()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Integrations\Traits;
4
5
use Integrations\Exceptions\AccessDeniedException;
6
7
trait Access
8
{
9
    /**
10
     * Set an operation as having access using the Settings API.
11
     *
12
     * @param string $operation
13
     *
14
     * @return bool
15
     */
16
    public function allowAccess($operation)
17
    {
18
        foreach ((array) $operation as $op) {
19
            $this->set($op.'.access', true);
0 ignored issues
show
Bug introduced by
It seems like set() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
20
        }
21
22
        return $this->hasAccessToAll($operation);
0 ignored issues
show
Documentation introduced by
$operation is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
23
    }
24
25
    /**
26
     * Disable the access to a certain operation, or the current one.
27
     *
28
     * @param bool $operation [description]
29
     *
30
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
31
     */
32
    public function denyAccess($operation)
33
    {
34
        foreach ((array) $operation as $op) {
35
            $this->set($op.'.access', false);
0 ignored issues
show
Bug introduced by
It seems like set() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
36
        }
37
38
        return ! $this->hasAccessToAny($operation);
0 ignored issues
show
Documentation introduced by
$operation is of type boolean, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
39
    }
40
41
    /**
42
     * Check if a operation is allowed for a Crud Panel. Return false if not.
43
     *
44
     * @param string $operation
45
     *
46
     * @return bool
47
     */
48
    public function hasAccess($operation)
49
    {
50
        return $this->get($operation.'.access') ?? false;
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
51
    }
52
53
    /**
54
     * Check if any operations are allowed for a Crud Panel. Return false if not.
55
     *
56
     * @param array $operation_array
57
     *
58
     * @return bool
59
     */
60 View Code Duplication
    public function hasAccessToAny($operation_array)
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...
61
    {
62
        foreach ((array) $operation_array as $key => $operation) {
63
            if ($this->get($operation.'.access') == true) {
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
64
                return true;
65
            }
66
        }
67
68
        return false;
69
    }
70
71
    /**
72
     * Check if all operations are allowed for a Crud Panel. Return false if not.
73
     *
74
     * @param array $operation_array Permissions.
75
     *
76
     * @return bool
77
     */
78 View Code Duplication
    public function hasAccessToAll($operation_array)
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...
79
    {
80
        foreach ((array) $operation_array as $key => $operation) {
81
            if (! $this->get($operation.'.access')) {
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
82
                return false;
83
            }
84
        }
85
86
        return true;
87
    }
88
89
    /**
90
     * Check if a operation is allowed for a Crud Panel. Fail if not.
91
     *
92
     * @param string $operation
93
     *
94
     * @throws \Backpack\CRUD\Exception\AccessDeniedException in case the operation is not enabled
95
     *
96
     * @return bool
97
     */
98
    public function hasAccessOrFail($operation)
99
    {
100
        if (! $this->get($operation.'.access')) {
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
101
            throw new AccessDeniedException(trans('backpack::crud.unauthorized_access', ['access' => $operation]));
102
        }
103
104
        return true;
105
    }
106
}
107