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

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