Code Duplication    Length = 44-45 lines in 2 locations

src/Traits/HasPermissions.php 1 location

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

src/Traits/HasRoles.php 1 location

@@ 139-182 (lines=44) @@
136
     *
137
     * @return $this
138
     */
139
    public function assignRole(...$roles)
140
    {
141
        $roles = collect($roles)
142
            ->flatten()
143
            ->map(function ($role) {
144
                if (empty($role)) {
145
                    return false;
146
                }
147
148
                return $this->getStoredRole($role);
149
            })
150
            ->filter(function ($role) {
151
                return $role instanceof Role;
152
            })
153
            ->each(function ($role) {
154
                $this->ensureModelSharesGuard($role);
155
            })
156
            ->map->id
157
            ->all();
158
159
        $model = $this->getModel();
160
161
        if ($model->exists) {
162
            $this->roles()->sync($roles, false);
163
            $model->load('roles');
164
        } else {
165
            $class = \get_class($model);
166
167
            $class::saved(
168
                function ($object) use ($roles, $model) {
169
                    static $modelLastFiredOn;
170
                    if ($modelLastFiredOn !== null && $modelLastFiredOn === $model) {
171
                        return;
172
                    }
173
                    $object->roles()->sync($roles, false);
174
                    $object->load('roles');
175
                    $modelLastFiredOn = $object;
176
                });
177
        }
178
179
        $this->forgetCachedPermissions();
180
181
        return $this;
182
    }
183
184
    /**
185
     * Revoke the given role from the model.