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

@@ 310-354 (lines=45) @@
307
     *
308
     * @return $this
309
     */
310
    public function givePermissionTo(...$permissions)
311
    {
312
        $permissions = collect($permissions)
313
            ->flatten()
314
            ->map(function ($permission) {
315
                if (empty($permission)) {
316
                    return false;
317
                }
318
319
                return $this->getStoredPermission($permission);
320
            })
321
            ->filter(function ($permission) {
322
                return $permission instanceof Permission;
323
            })
324
            ->each(function ($permission) {
325
                $this->ensureModelSharesGuard($permission);
326
            })
327
            ->map->id
328
            ->all();
329
330
        $model = $this->getModel();
331
332
        if ($model->exists) {
333
            $this->permissions()->sync($permissions, false);
334
            $model->load('permissions');
335
        } else {
336
            $class = \get_class($model);
337
338
            $class::saved(
339
                function ($object) use ($permissions, $model) {
340
                    static $modelLastFiredOn;
341
                    if ($modelLastFiredOn !== null && $modelLastFiredOn === $model) {
342
                        return;
343
                    }
344
                    $object->permissions()->sync($permissions, false);
345
                    $object->load('permissions');
346
                    $modelLastFiredOn = $object;
347
                }
348
            );
349
        }
350
351
        $this->forgetCachedPermissions();
352
353
        return $this;
354
    }
355
356
    /**
357
     * Remove all current permissions and set the given ones.