Code Duplication    Length = 44-45 lines in 2 locations

src/Traits/HasRoles.php 1 location

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

src/Traits/HasPermissions.php 1 location

@@ 276-320 (lines=45) @@
273
     *
274
     * @return $this
275
     */
276
    public function givePermissionTo(...$permissions)
277
    {
278
        $permissions = collect($permissions)
279
            ->flatten()
280
            ->map(function ($permission) {
281
                if (empty($permission)) {
282
                    return false;
283
                }
284
285
                return $this->getStoredPermission($permission);
286
            })
287
            ->filter(function ($permission) {
288
                return $permission instanceof Permission;
289
            })
290
            ->each(function ($permission) {
291
                $this->ensureModelSharesGuard($permission);
292
            })
293
            ->map->id
294
            ->all();
295
296
        $model = $this->getModel();
297
298
        if ($model->exists) {
299
            $this->permissions()->sync($permissions, false);
300
            $model->load('permissions');
301
        } else {
302
            $class = \get_class($model);
303
304
            $class::saved(
305
                function ($object) use ($permissions, $model) {
306
                    static $modelLastFiredOn;
307
                    if ($modelLastFiredOn !== null && $modelLastFiredOn === $model) {
308
                        return;
309
                    }
310
                    $object->permissions()->sync($permissions, false);
311
                    $object->load('permissions');
312
                    $modelLastFiredOn = $object;
313
                }
314
            );
315
        }
316
317
        $this->forgetCachedPermissions();
318
319
        return $this;
320
    }
321
322
    /**
323
     * Remove all current permissions and set the given ones.