Code Duplication    Length = 40-44 lines in 2 locations

src/Traits/HasPermissions.php 1 location

@@ 397-436 (lines=40) @@
394
     *
395
     * @return $this
396
     */
397
    public function givePermissionTo(...$permissions)
398
    {
399
        $permissions = collect($permissions)
400
            ->flatten()
401
            ->map(function ($permission) {
402
                return $this->getStoredPermission($permission);
403
            })
404
            ->filter(function ($permission) {
405
                return $permission instanceof Permission;
406
            })
407
            ->each(function ($permission) {
408
                $this->ensureModelSharesGuard($permission);
409
            })
410
            ->map->id
411
            ->all();
412
413
        $model = $this->getModel();
414
415
        if ($model->exists) {
416
            $this->permissions()->sync($permissions, false);
417
            $model->load('permissions');
418
        } else {
419
            $class = \get_class($model);
420
421
            $class::saved(
422
                function ($object) use ($permissions, $model) {
423
                    static $modelLastFiredOn;
424
                    if ($modelLastFiredOn !== null && $modelLastFiredOn === $model) {
425
                        return;
426
                    }
427
                    $object->permissions()->sync($permissions, false);
428
                    $object->load('permissions');
429
                    $modelLastFiredOn = $object;
430
                });
431
        }
432
433
        $this->forgetCachedPermissions();
434
435
        return $this;
436
    }
437
438
    /**
439
     * Remove all current permissions and set the given ones.

src/Traits/HasRoles.php 1 location

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