Code Duplication    Length = 46-47 lines in 2 locations

src/Traits/HasPermissions.php 1 location

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

src/Traits/HasRoles.php 1 location

@@ 97-142 (lines=46) @@
94
     *
95
     * @return $this
96
     */
97
    public function assignRole(...$roles)
98
    {
99
        $roles = collect($roles)
100
            ->flatten()
101
            ->map(function ($role) {
102
                if (empty($role)) {
103
                    return false;
104
                }
105
106
                return $this->getStoredRole($role);
107
            })
108
            ->filter(function ($role) {
109
                return $role instanceof Role &&
110
                       $role->company == $this->getCompany();
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
139
        $this->forgetCachedPermissions();
140
141
        return $this;
142
    }
143
144
    /**
145
     * Revoke the given role from the model.