Code Duplication    Length = 44-45 lines in 2 locations

src/Traits/HasRoles.php 1 location

@@ 97-140 (lines=44) @@
94
     *
95
     * @return $this
96
     */
97
    public function assignRole(...$roles)
98
    {
99
        $roles = collect($roles)
100
            ->flatten()
101
            ->map(function ($role) {
102
                if (empty($role)) {
103
                    return false;
104
                }
105
106
                return $this->getStoredRole($role);
107
            })
108
            ->filter(function ($role) {
109
                return $role instanceof Role;
110
            })
111
            ->each(function ($role) {
112
                $this->ensureModelSharesGuard($role);
113
            })
114
            ->map->id
115
            ->all();
116
117
        $model = $this->getModel();
118
119
        if ($model->exists) {
120
            $this->roles()->sync($roles, false);
121
            $model->load('roles');
122
        } else {
123
            $class = \get_class($model);
124
125
            $class::saved(
126
                function ($object) use ($roles, $model) {
127
                    static $modelLastFiredOn;
128
                    if ($modelLastFiredOn !== null && $modelLastFiredOn === $model) {
129
                        return;
130
                    }
131
                    $object->roles()->sync($roles, false);
132
                    $object->load('roles');
133
                    $modelLastFiredOn = $object;
134
                });
135
        }
136
137
        $this->forgetCachedPermissions();
138
139
        return $this;
140
    }
141
142
    /**
143
     * Revoke the given role from the model.

src/Traits/HasPermissions.php 1 location

@@ 301-345 (lines=45) @@
298
     *
299
     * @return $this
300
     */
301
    public function givePermissionTo(...$permissions)
302
    {
303
        $permissions = collect($permissions)
304
            ->flatten()
305
            ->map(function ($permission) {
306
                if (empty($permission)) {
307
                    return false;
308
                }
309
310
                return $this->getStoredPermission($permission);
311
            })
312
            ->filter(function ($permission) {
313
                return $permission instanceof Permission;
314
            })
315
            ->each(function ($permission) {
316
                $this->ensureModelSharesGuard($permission);
317
            })
318
            ->map->id
319
            ->all();
320
321
        $model = $this->getModel();
322
323
        if ($model->exists) {
324
            $this->permissions()->sync($permissions, false);
325
            $model->load('permissions');
326
        } else {
327
            $class = \get_class($model);
328
329
            $class::saved(
330
                function ($object) use ($permissions, $model) {
331
                    static $modelLastFiredOn;
332
                    if ($modelLastFiredOn !== null && $modelLastFiredOn === $model) {
333
                        return;
334
                    }
335
                    $object->permissions()->sync($permissions, false);
336
                    $object->load('permissions');
337
                    $modelLastFiredOn = $object;
338
                }
339
            );
340
        }
341
342
        $this->forgetCachedPermissions();
343
344
        return $this;
345
    }
346
347
    /**
348
     * Remove all current permissions and set the given ones.