Completed
Push — master ( f9e78a...edb43e )
by Abdelrahman
09:22
created

HasAbilities::syncAbilities()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Fort Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Fort Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
namespace Rinvex\Fort\Traits;
17
18
use Rinvex\Fort\Models\Ability;
19
20
trait HasAbilities
21
{
22
    /**
23
     * Attach the given abilities to the model.
24
     *
25
     * @param string|array $action
26
     * @param string|array $resource
27
     *
28
     * @return $this
29
     */
30
    public function grantAbilities($action, $resource)
31
    {
32
        $this->setAbilities($action, $resource, 'syncWithoutDetaching');
33
34
        return $this;
35
    }
36
37
    /**
38
     * Sync the given abilities to the model.
39
     *
40
     * @param string|array $action
41
     * @param string|array $resource
42
     *
43
     * @return $this
44
     */
45
    public function syncAbilities($action, $resource)
46
    {
47
        $this->setAbilities($action, $resource, 'sync');
48
49
        return $this;
50
    }
51
52
    /**
53
     * Detach the given abilities from the model.
54
     *
55
     * @param string|array $action
56
     * @param string|array $resource
57
     *
58
     * @return $this
59
     */
60
    public function revokeAbilities($action, $resource)
61
    {
62
        $this->setAbilities($action, $resource, 'detach');
63
64
        return $this;
65
    }
66
67
    /**
68
     * Set the given ability(s) to the model.
69
     *
70
     * @param string|array $action
71
     * @param string|array $resource
72
     * @param string       $process
73
     *
74
     * @return bool
75
     */
76
    protected function setAbilities($action, $resource, string $process)
77
    {
78
        // Guess event name
79
        $event = $process == 'syncWithoutDetaching' ? 'attach' : $process;
80
81
        // If the "attaching/syncing/detaching" event returns false we'll cancel this operation and
82
        // return false, indicating that the attaching/syncing/detaching failed. This provides a
83
        // chance for any listeners to cancel save operations if validations fail or whatever.
84
        if ($this->fireModelEvent($event.'ing') === false) {
0 ignored issues
show
Bug introduced by
It seems like fireModelEvent() 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...
85
            return false;
86
        }
87
88
        // Ability model
89
        $model = Ability::query();
90
91
        if (is_string($action) && $action !== '*') {
92
            $model->where('action', $action);
93
        }
94
95
        if (is_array($action)) {
96
            $model->whereIn('action', $action);
97
        }
98
99
        if (is_string($resource) && $resource !== '*') {
100
            $model->where('resource', $resource);
101
        }
102
103
        if (is_array($resource)) {
104
            $model->whereIn('resource', $resource);
105
        }
106
107
        // Find the given abilities
108
        $abilities = $model->get();
109
110
        // Sync abilities
111
        $this->abilities()->$process($abilities);
0 ignored issues
show
Bug introduced by
The method abilities() does not exist on Rinvex\Fort\Traits\HasAbilities. Did you maybe mean grantAbilities()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
112
113
        // Fire the roles attached/synced/detached event
114
        $this->fireModelEvent($event.'ed', false);
0 ignored issues
show
Bug introduced by
It seems like fireModelEvent() 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...
115
116
        return true;
117
    }
118
}
119