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

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