Code Duplication    Length = 44-45 lines in 2 locations

src/Traits/HasPermissions.php 1 location

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

src/Traits/HasRoles.php 1 location

@@ 108-151 (lines=44) @@
105
     *
106
     * @return $this
107
     */
108
    public function assignRole(...$roles)
109
    {
110
        $roles = collect($roles)
111
            ->flatten()
112
            ->map(function ($role) {
113
                if (empty($role)) {
114
                    return false;
115
                }
116
117
                return $this->getStoredRole($role);
118
            })
119
            ->filter(function ($role) {
120
                return $role instanceof Role;
121
            })
122
            ->each(function ($role) {
123
                $this->ensureModelSharesGuard($role);
124
            })
125
            ->map->id
126
            ->all();
127
128
        $model = $this->getModel();
129
130
        if ($model->exists) {
131
            $this->roles()->sync($roles, false);
132
            $model->load('roles');
133
        } else {
134
            $class = \get_class($model);
135
136
            $class::saved(
137
                function ($object) use ($roles, $model) {
138
                    static $modelLastFiredOn;
139
                    if ($modelLastFiredOn !== null && $modelLastFiredOn === $model) {
140
                        return;
141
                    }
142
                    $object->roles()->sync($roles, false);
143
                    $object->load('roles');
144
                    $modelLastFiredOn = $object;
145
                }
146
            );
147
        }
148
149
        $this->forgetCachedPermissions();
150
151
        return $this;
152
    }
153
154
    /**