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

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