andrey-helldar /
laravel-roles
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Helldar\Roles\Traits; |
||
| 4 | |||
| 5 | use Closure; |
||
| 6 | use Helldar\Roles\Exceptions\Core\PermissionNotFoundException; |
||
| 7 | use Helldar\Roles\Exceptions\Core\RoleNotFoundException; |
||
| 8 | use Helldar\Roles\Facades\Database\Search; |
||
| 9 | use Helldar\Roles\Models\Permission; |
||
| 10 | use Helldar\Roles\Models\Role; |
||
| 11 | use Illuminate\Database\Eloquent\Builder; |
||
| 12 | use Illuminate\Database\Eloquent\Model; |
||
| 13 | |||
| 14 | trait Searchable |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @param \Helldar\Roles\Models\Role|string $role |
||
| 18 | * |
||
| 19 | * @throws \Throwable |
||
| 20 | * |
||
| 21 | * @return \Illuminate\Database\Eloquent\Model |
||
| 22 | */ |
||
| 23 | 30 | protected function findRole($role) |
|
| 24 | { |
||
| 25 | 30 | return $this->findObject(Role::class, $role, RoleNotFoundException::class); |
|
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param \Helldar\Roles\Models\Permission|string $permission |
||
| 30 | * |
||
| 31 | * @throws \Throwable |
||
| 32 | * |
||
| 33 | * @return \Illuminate\Database\Eloquent\Model |
||
| 34 | */ |
||
| 35 | 24 | protected function findPermission($permission) |
|
| 36 | { |
||
| 37 | 24 | return $this->findObject(Permission::class, $permission, PermissionNotFoundException::class); |
|
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param \Illuminate\Database\Eloquent\Model|string $model |
||
| 42 | * @param mixed $value |
||
| 43 | * @param string $exception |
||
| 44 | * |
||
| 45 | * @throws \Throwable |
||
| 46 | * |
||
| 47 | * @return \Illuminate\Database\Eloquent\Model |
||
| 48 | */ |
||
| 49 | 54 | protected function findObject(string $model, $value, string $exception) |
|
| 50 | { |
||
| 51 | 54 | if ($value instanceof $model) { |
|
| 52 | return $value; |
||
| 53 | } |
||
| 54 | |||
| 55 | return $this->searchOr($model, $value, function () use ($exception, $value) { |
||
| 56 | throw new $exception($value); |
||
| 57 | 54 | }); |
|
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param \Helldar\Roles\Models\BaseModel|string $model |
||
| 62 | * @param mixed $value |
||
| 63 | * |
||
| 64 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 65 | */ |
||
| 66 | 54 | protected function searchBuilder($model, $value): Builder |
|
| 67 | { |
||
| 68 | 54 | return Search::by($model::query(), $value); |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param \Illuminate\Database\Eloquent\Model|string $model |
||
| 73 | * @param mixed $value |
||
| 74 | * |
||
| 75 | * @return bool |
||
| 76 | */ |
||
| 77 | protected function searchExist($model, $value): bool |
||
| 78 | { |
||
| 79 | return $this->searchBuilder($model, $value)->exists(); |
||
|
0 ignored issues
–
show
|
|||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param \Illuminate\Database\Eloquent\Model|string $model |
||
| 84 | * @param mixed $value |
||
| 85 | * @param \Closure $callback |
||
| 86 | * |
||
| 87 | * @return \Illuminate\Database\Eloquent\Model |
||
| 88 | */ |
||
| 89 | 54 | protected function searchOr($model, $value, Closure $callback): Model |
|
| 90 | { |
||
| 91 | 54 | return $this->searchBuilder($model, $value)->firstOr(['*'], $callback); |
|
| 92 | } |
||
| 93 | } |
||
| 94 |