Code Duplication    Length = 35-38 lines in 2 locations

src/Traits/HasRoles.php 1 location

@@ 95-132 (lines=38) @@
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 ($model) use ($roles) {
125
                    $model->roles()->sync($roles, false);
126
                });
127
        }
128
129
        $this->forgetCachedPermissions();
130
131
        return $this;
132
    }
133
134
    /**
135
     * Revoke the given role from the model.

src/Traits/HasPermissions.php 1 location

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