Completed
Push — master ( 7c1cbc...aa55a0 )
by Abdelrahman
08:51
created

HasAbilities::hasDirectAbility()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 24
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 10
nc 8
nop 1
dl 0
loc 24
rs 5.7377
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
use Illuminate\Support\Collection;
20
21
trait HasAbilities
22
{
23
    /**
24
     * Give the given ability to a role/user.
25
     *
26
     * @param string|array|\Rinvex\Fort\Models\Ability|\Illuminate\Support\Collection $ability
27
     *
28
     * @return $this
29
     */
30
    public function giveAbilityTo($ability)
31
    {
32
        $origAbility = $ability;
33
34
        // Fire the ability giving event
35
        event('rinvex.fort.ability.giving', [$origAbility, $this]);
36
37
        // Single ability slug
38
        if (is_string($ability)) {
39
            $ability = app('rinvex.fort.ability')->whereSlug($ability)->first();
40
        }
41
42
        // Single ability model
43
        if ($ability instanceof Ability) {
44
            if ($this->hasAbilityTo($ability)) {
45
                return $this;
46
            }
47
48
            $this->abilities()->attach($ability);
0 ignored issues
show
Bug introduced by
It seems like abilities() 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...
49
        }
50
51
        // Array of ability slugs
52
        if (is_array($ability)) {
53
            $ability = app('rinvex.fort.ability')->findWhereIn(['slug', $ability]);
54
        }
55
56
        // Collection of ability models
57
        if ($ability instanceof Collection) {
58
            $ability = $ability->map(function ($ability) {
59
                return $ability instanceof Ability ? $ability->id : $ability;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Rinvex\Fort\Models\Ability>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
60
            })->toArray();
61
62
            $this->abilities()->syncWithoutDetaching($ability);
0 ignored issues
show
Bug introduced by
It seems like abilities() 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...
63
        }
64
65
        // Fire the ability given event
66
        event('rinvex.fort.ability.given', [$origAbility, $this]);
67
68
        return $this;
69
    }
70
71
    /**
72
     * Revoke the given ability from a role/user.
73
     *
74
     * @param string|array|\Rinvex\Fort\Models\Ability|\Illuminate\Support\Collection $ability
75
     *
76
     * @return $this
77
     */
78
    public function revokeAbilityTo($ability)
79
    {
80
        $origAbility = $ability;
81
82
        // Fire the ability revoking event
83
        event('rinvex.fort.ability.revoking', [$origAbility, $this]);
84
85
        // Single ability slug
86
        if (is_string($ability)) {
87
            $ability = $this->abilities()->whereSlug($ability)->first();
0 ignored issues
show
Bug introduced by
It seems like abilities() 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...
88
        }
89
90
        // Single ability model
91
        if ($ability instanceof Ability) {
92
            if (! $this->hasAbilityTo($ability)) {
93
                return $this;
94
            }
95
96
            $this->abilities()->detach($ability);
0 ignored issues
show
Bug introduced by
It seems like abilities() 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...
97
        }
98
99
        // Array of ability slugs
100
        if (is_array($ability)) {
101
            $ability = app('rinvex.fort.ability')->findWhereIn(['slug', $ability]);
102
        }
103
104
        // Collection of ability models
105
        if ($ability instanceof Collection) {
106
            $remove = $ability->map(function ($ability) {
107
                return $ability instanceof Ability ? $ability->id : $ability;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Rinvex\Fort\Models\Ability>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
108
            })->toArray();
109
110
            $this->abilities()->sync(array_diff($this->abilities()->getRelatedIds()->toArray(), $remove));
0 ignored issues
show
Bug introduced by
It seems like abilities() 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...
111
        }
112
113
        // Fire the ability revoked event
114
        event('rinvex.fort.ability.revoked', [$origAbility, $this]);
115
116
        return $this;
117
    }
118
119
    /**
120
     * Determine if the user may perform the given ability.
121
     *
122
     * @param string|array|\Rinvex\Fort\Models\Ability|\Illuminate\Support\Collection $role
0 ignored issues
show
Bug introduced by
There is no parameter named $role. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
123
     *
124
     * @return bool
125
     */
126
    public function hasAbilityTo($ability)
127
    {
128
        return $this->hasDirectAbility($ability);
129
    }
130
131
    /**
132
     * Determine if the user has the given ability.
133
     *
134
     * @param string|array|\Rinvex\Fort\Models\Ability|\Illuminate\Support\Collection $role
0 ignored issues
show
Bug introduced by
There is no parameter named $role. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
135
     *
136
     * @return bool
137
     */
138
    protected function hasDirectAbility($ability)
139
    {
140
        // Single ability slug
141
        if (is_string($ability)) {
142
            return $this->abilities()->whereSlug($ability)->first() ? true : false;
0 ignored issues
show
Bug introduced by
It seems like abilities() 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...
143
        }
144
145
        // Single ability model
146
        if ($ability instanceof Ability) {
147
            return $this->abilities->contains('slug', $ability->slug);
0 ignored issues
show
Bug introduced by
The property abilities 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...
Documentation introduced by
The property slug does not exist on object<Rinvex\Fort\Models\Ability>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
148
        }
149
150
        // Array of ability slugs
151
        if (is_array($ability)) {
152
            return $this->abilities->pluck('slug')->intersect($ability)->isEmpty() ? false : true;
153
        }
154
155
        // Collection of role models
156
        if ($ability instanceof Collection) {
157
            return $this->abilities->pluck('slug')->intersect($ability->pluck('slug')->toArray())->isEmpty() ? false : true;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
158
        }
159
160
        return false;
161
    }
162
}
163