Completed
Push — master ( 17a334...ed601b )
by Mahmoud
02:57
created

RequestTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 37
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B validatePermission() 0 24 4
1
<?php
2
3
namespace App\Port\Request\Traits;
4
5
/**
6
 * Class RequestTrait
7
 *
8
 * @author  Mahmoud Zalt  <[email protected]>
9
 */
10
trait RequestTrait
11
{
12
13
    /**
14
     * This function will be called from the Requests (authorize) to check if a user
15
     * has permission to perform an action.
16
     * User can set multiple permissions (separated with "|") and if the user has
17
     * any of the permissions, he will be authorize to proceed with this action.
18
     *
19
     * @return  bool
20
     */
21
    public function validatePermission()
22
    {
23
        // $this->access is optionally set on the Request
24
25
        // allow access when the access is not defined
26
        // allow access when nothing no roles or permissions are declared
27
        if (!isset($this->access) || !isset($this->access['permission'])) {
0 ignored issues
show
Bug introduced by
The property access does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28
            return true;
29
        }
30
31
        // allow access if has permission set but is empty or null
32
        if(!$this->access['permission']){
33
            return true;
34
        }
35
36
        $permissions = explode('|', $this->access['permission']);
37
38
        $hasPermission = array_map(function($permission) {
39
            return $this->user()->hasPermissionTo($permission);
0 ignored issues
show
Bug introduced by
It seems like user() 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...
40
        }, $permissions);
41
42
        // allow access if user has access to any of the defined permissions.
43
        return in_array(true, $hasPermission);
44
    }
45
46
}
47