authorizePermissionResource()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 11
cp 0
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
namespace Yajra\Acl\Traits;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Fluent;
7
8
/**
9
 * @mixin \Illuminate\Routing\Controller
10
 */
11
trait AuthorizesPermissionResources
12
{
13
    /**
14
     * Permission resource ability mapping.
15
     *
16
     * @var array
17
     */
18
    protected $resourcePermissionMap = [
19
        'index' => 'viewAny',
20
        'create' => 'create',
21
        'store' => 'create',
22
        'show' => 'view',
23
        'edit' => 'update',
24
        'update' => 'update',
25
        'destroy' => 'delete',
26
    ];
27
28
    /**
29
     * Authorize a permission resource action based on the incoming request.
30
     *
31
     * @param  string  $resource
32
     * @param  array  $options
33
     * @return void
34
     */
35
    public function authorizePermissionResource(string $resource, array $options = [])
36
    {
37
        $permissions = $this->resourcePermissionMap();
38
        $collection = new Collection;
39
        foreach ($permissions as $method => $ability) {
40
            $collection->push(new Fluent([
41
                'ability' => $ability,
42
                'method' => $method,
43
            ]));
44
        }
45
46
        $collection->groupBy('ability')->each(function ($permission, $ability) use ($resource, $options) {
47
            $this->middleware("can:{$resource}.{$ability}")
0 ignored issues
show
Bug introduced by
It seems like middleware() 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...
48
                ->only($permission->pluck('method')->toArray());
49
        });
50
    }
51
52
    /**
53
     * Get the map of permission resource methods to ability names.
54
     *
55
     * @return array
56
     */
57
    protected function resourcePermissionMap(): array
58
    {
59
        if (property_exists($this, 'customPermissionMap') && is_array($this->customPermissionMap)) {
60
            return array_merge($this->resourcePermissionMap, $this->customPermissionMap);
0 ignored issues
show
Bug introduced by
The property customPermissionMap 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...
61
        }
62
63
        return $this->resourcePermissionMap;
64
    }
65
}
66