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

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