Permission::users()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Maklad\Permission\Models;
4
5
use Illuminate\Support\Collection;
6
use Jenssegers\Mongodb\Eloquent\Model;
7
use Jenssegers\Mongodb\Relations\BelongsToMany;
8
use Maklad\Permission\Contracts\PermissionInterface;
9
use Maklad\Permission\Exceptions\PermissionAlreadyExists;
10
use Maklad\Permission\Exceptions\PermissionDoesNotExist;
11
use Maklad\Permission\Guard;
12
use Maklad\Permission\Helpers;
13
use Maklad\Permission\PermissionRegistrar;
14
use Maklad\Permission\Traits\HasRoles;
15
use Maklad\Permission\Traits\RefreshesPermissionCache;
16
17
/**
18
 * Class Permission
19
 * @package Maklad\Permission\Models
20
 */
21
class Permission extends Model implements PermissionInterface
22
{
23
    use HasRoles;
0 ignored issues
show
introduced by
The trait Maklad\Permission\Traits\HasRoles requires some properties which are not provided by Maklad\Permission\Models\Permission: $name, $permissions, $roles, $forceDeleting, $guard_name
Loading history...
24
    use RefreshesPermissionCache;
25
26
    public $guarded = ['id'];
27
    protected $helpers;
28
29
    /**
30
     * Permission constructor.
31
     *
32
     * @param array $attributes
33
     *
34
     * @throws \ReflectionException
35
     */
36 123
    public function __construct(array $attributes = [])
37
    {
38 123
        $attributes['guard_name'] = $attributes['guard_name'] ?? (new Guard())->getDefaultName(static::class);
39
40 123
        parent::__construct($attributes);
41
42 123
        $this->helpers = new Helpers();
43
44 123
        $this->setTable(config('permission.collection_names.permissions'));
45 123
    }
46
47
    /**
48
     * Create new Permission
49
     *
50
     * @param array $attributes
51
     *
52
     * @return $this|\Illuminate\Database\Eloquent\Model
53
     * @throws \Maklad\Permission\Exceptions\PermissionAlreadyExists
54
     * @throws \ReflectionException
55
     */
56 123
    public static function create(array $attributes = [])
57
    {
58 123
        $helpers = new Helpers();
59 123
        $attributes['guard_name'] = $attributes['guard_name'] ?? (new Guard())->getDefaultName(static::class);
60
61 123
        if (static::getPermissions()->where('name', $attributes['name'])->where(
62 123
            'guard_name',
63 123
            $attributes['guard_name']
64 123
        )->first()) {
65 1
            $name = (string)$attributes['name'];
66 1
            $guardName = (string)$attributes['guard_name'];
67 1
            throw new PermissionAlreadyExists($helpers->getPermissionAlreadyExistsMessage($name, $guardName));
68
        }
69
70 123
        return $helpers->checkVersion() ? parent::create($attributes) : static::query()->create($attributes);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Jenssegers\Mongodb\Eloquent\Model. Did you maybe mean created()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        return $helpers->checkVersion() ? parent::/** @scrutinizer ignore-call */ create($attributes) : static::query()->create($attributes);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
    }
72
73
    /**
74 123
     * Find or create permission by its name (and optionally guardName).
75
     *
76
     * @param string $name
77
     * @param string $guardName
78
     *
79
     * @return PermissionInterface
80
     * @throws \Maklad\Permission\Exceptions\PermissionAlreadyExists
81
     * @throws \ReflectionException
82
     */
83
    public static function findOrCreate(string $name, string $guardName = null): PermissionInterface
84
    {
85
        $guardName = $guardName ?? (new Guard())->getDefaultName(static::class);
86
87 1
        $permission = static::getPermissions()->filter(function ($permission) use ($name, $guardName) {
88
            return $permission->name === $name && $permission->guard_name === $guardName;
89 1
        })->first();
90
91 1
        if (!$permission) {
92 1
            $permission = static::create(['name' => $name, 'guard_name' => $guardName]);
93 1
        }
94 1
95
        return $permission;
96 1
    }
97 1
98
    /**
99
     * A permission can be applied to roles.
100 1
     * @return BelongsToMany
101
     */
102
    public function roles(): BelongsToMany
103
    {
104
        return $this->belongsToMany(config('permission.models.role'));
105
    }
106
107 123
    /**
108
     * A permission belongs to some users of the model associated with its guard.
109 123
     * @return BelongsToMany
110
     */
111
    public function users(): BelongsToMany
112
    {
113
        return $this->belongsToMany($this->helpers->getModelForGuard($this->attributes['guard_name']));
114
    }
115
116 2
    /**
117
     * Find a permission by its name (and optionally guardName).
118 2
     *
119
     * @param string $name
120
     * @param string $guardName
121
     *
122
     * @return PermissionInterface
123
     * @throws PermissionDoesNotExist
124
     * @throws \ReflectionException
125
     */
126
    public static function findByName(string $name, string $guardName = null): PermissionInterface
127
    {
128
        $guardName = $guardName ?? (new Guard())->getDefaultName(static::class);
129
130
        $permission = static::getPermissions()->filter(function ($permission) use ($name, $guardName) {
131 44
            return $permission->name === $name && $permission->guard_name === $guardName;
132
        })->first();
133 44
134
        if (!$permission) {
135 44
            $helpers = new Helpers();
136
            throw new PermissionDoesNotExist($helpers->getPermissionDoesNotExistMessage($name, $guardName));
137 44
        }
138 7
139 7
        return $permission;
140
    }
141
142 38
    /**
143
     * Get the current cached permissions.
144
     * @return Collection
145
     */
146
    protected static function getPermissions(): Collection
147
    {
148
        return \app(PermissionRegistrar::class)->getPermissions();
149 123
    }
150
}
151