Code Duplication    Length = 44-45 lines in 2 locations

src/Traits/HasRoles.php 1 location

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

src/Traits/HasPermissions.php 1 location

@@ 364-408 (lines=45) @@
361
     *
362
     * @return $this
363
     */
364
    public function givePermissionTo(...$permissions)
365
    {
366
        $permissions = collect($permissions)
367
            ->flatten()
368
            ->map(function ($permission) {
369
                if (empty($permission)) {
370
                    return false;
371
                }
372
373
                return $this->getStoredPermission($permission);
374
            })
375
            ->filter(function ($permission) {
376
                return $permission instanceof Permission;
377
            })
378
            ->each(function ($permission) {
379
                $this->ensureModelSharesGuard($permission);
380
            })
381
            ->map->id
382
            ->all();
383
384
        $model = $this->getModel();
385
386
        if ($model->exists) {
387
            $this->permissions()->sync($permissions, false);
388
            $model->load('permissions');
389
        } else {
390
            $class = \get_class($model);
391
392
            $class::saved(
393
                function ($object) use ($permissions, $model) {
394
                    static $modelLastFiredOn;
395
                    if ($modelLastFiredOn !== null && $modelLastFiredOn === $model) {
396
                        return;
397
                    }
398
                    $object->permissions()->sync($permissions, false);
399
                    $object->load('permissions');
400
                    $modelLastFiredOn = $object;
401
                }
402
            );
403
        }
404
405
        $this->forgetCachedPermissions();
406
407
        return $this;
408
    }
409
410
    /**
411
     * Remove all current permissions and set the given ones.