This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Spatie\ModelStatus; |
||
| 4 | |||
| 5 | use Illuminate\Database\Eloquent\Builder; |
||
| 6 | use Illuminate\Database\Eloquent\Relations\MorphMany; |
||
| 7 | use Illuminate\Database\Eloquent\Relations\Relation; |
||
| 8 | use Illuminate\Database\Query\Builder as QueryBuilder; |
||
| 9 | use Illuminate\Support\Arr; |
||
| 10 | use Illuminate\Support\Facades\DB; |
||
| 11 | use Spatie\ModelStatus\Events\StatusUpdated; |
||
| 12 | use Spatie\ModelStatus\Exceptions\InvalidStatus; |
||
| 13 | |||
| 14 | trait HasStatuses |
||
| 15 | { |
||
| 16 | public function statuses(): MorphMany |
||
| 17 | { |
||
| 18 | return $this->morphMany($this->getStatusModelClassName(), 'model', 'model_type', $this->getModelKeyColumnName()) |
||
| 19 | ->latest('id'); |
||
| 20 | } |
||
| 21 | |||
| 22 | public function status(): ?Status |
||
| 23 | { |
||
| 24 | return $this->latestStatus(); |
||
| 25 | } |
||
| 26 | |||
| 27 | public function setStatus(string $name, ?string $reason = null): self |
||
| 28 | { |
||
| 29 | if (! $this->isValidStatus($name, $reason)) { |
||
| 30 | throw InvalidStatus::create($name); |
||
| 31 | } |
||
| 32 | |||
| 33 | return $this->forceSetStatus($name, $reason); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function isValidStatus(string $name, ?string $reason = null): bool |
||
|
0 ignored issues
–
show
|
|||
| 37 | { |
||
| 38 | return true; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param string|array $names |
||
| 43 | * |
||
| 44 | * @return null|Status |
||
| 45 | */ |
||
| 46 | public function latestStatus(...$names): ?Status |
||
| 47 | { |
||
| 48 | $statuses = $this->relationLoaded('statuses') ? $this->statuses : $this->statuses(); |
||
| 49 | |||
| 50 | $names = is_array($names) ? Arr::flatten($names) : func_get_args(); |
||
| 51 | if (count($names) < 1) { |
||
| 52 | return $statuses->first(); |
||
| 53 | } |
||
| 54 | |||
| 55 | return $statuses->whereIn('name', $names)->first(); |
||
| 56 | } |
||
| 57 | |||
| 58 | public function hasEverHadStatus($name): bool |
||
| 59 | { |
||
| 60 | $statuses = $this->relationLoaded('statuses') ? $this->statuses : $this->statuses(); |
||
| 61 | |||
| 62 | return $statuses->where('name', $name)->count() > 0; |
||
| 63 | } |
||
| 64 | |||
| 65 | public function deleteStatus(...$names) |
||
| 66 | { |
||
| 67 | $names = is_array($names) ? Arr::flatten($names) : func_get_args(); |
||
| 68 | if (count($names) < 1) { |
||
| 69 | return $this; |
||
| 70 | } |
||
| 71 | |||
| 72 | $this->statuses()->whereIn('name', $names)->delete(); |
||
| 73 | } |
||
| 74 | |||
| 75 | public function scopeCurrentStatus(Builder $builder, ...$names) |
||
| 76 | { |
||
| 77 | $names = is_array($names) ? Arr::flatten($names) : func_get_args(); |
||
| 78 | $builder |
||
| 79 | ->whereHas( |
||
| 80 | 'statuses', |
||
| 81 | function (Builder $query) use ($names) { |
||
| 82 | $query |
||
| 83 | ->whereIn('name', $names) |
||
| 84 | ->whereIn( |
||
| 85 | 'id', |
||
| 86 | function (QueryBuilder $query) { |
||
| 87 | $query |
||
| 88 | ->select(DB::raw('max(id)')) |
||
| 89 | ->from($this->getStatusTableName()) |
||
| 90 | ->where('model_type', $this->getStatusModelType()) |
||
| 91 | ->whereColumn($this->getModelKeyColumnName(), $this->getQualifiedKeyName()); |
||
| 92 | } |
||
| 93 | ); |
||
| 94 | } |
||
| 95 | ); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param string|array $names |
||
| 100 | * |
||
| 101 | * @return void |
||
| 102 | **/ |
||
| 103 | public function scopeOtherCurrentStatus(Builder $builder, ...$names) |
||
| 104 | { |
||
| 105 | $names = is_array($names) ? Arr::flatten($names) : func_get_args(); |
||
| 106 | $builder |
||
| 107 | ->whereHas( |
||
| 108 | 'statuses', |
||
| 109 | function (Builder $query) use ($names) { |
||
| 110 | $query |
||
| 111 | ->whereNotIn('name', $names) |
||
| 112 | ->whereIn( |
||
| 113 | 'id', |
||
| 114 | function (QueryBuilder $query) use ($names) { |
||
| 115 | $query |
||
| 116 | ->select(DB::raw('max(id)')) |
||
| 117 | ->from($this->getStatusTableName()) |
||
| 118 | ->where('model_type', $this->getStatusModelType()) |
||
| 119 | ->whereColumn($this->getModelKeyColumnName(), $this->getQualifiedKeyName()); |
||
| 120 | } |
||
| 121 | ); |
||
| 122 | } |
||
| 123 | ) |
||
| 124 | ->orWhereDoesntHave('statuses'); |
||
| 125 | } |
||
| 126 | |||
| 127 | public function getStatusAttribute(): string |
||
| 128 | { |
||
| 129 | return (string) $this->latestStatus(); |
||
| 130 | } |
||
| 131 | |||
| 132 | public function forceSetStatus(string $name, ?string $reason = null): self |
||
| 133 | { |
||
| 134 | $oldStatus = $this->latestStatus(); |
||
| 135 | |||
| 136 | $newStatus = $this->statuses()->create([ |
||
| 137 | 'name' => $name, |
||
| 138 | 'reason' => $reason, |
||
| 139 | ]); |
||
| 140 | |||
| 141 | event(new StatusUpdated($oldStatus, $newStatus, $this)); |
||
| 142 | |||
| 143 | return $this; |
||
| 144 | } |
||
| 145 | |||
| 146 | protected function getStatusTableName(): string |
||
| 147 | { |
||
| 148 | $modelClass = $this->getStatusModelClassName(); |
||
| 149 | |||
| 150 | return (new $modelClass)->getTable(); |
||
| 151 | } |
||
| 152 | |||
| 153 | protected function getModelKeyColumnName(): string |
||
| 154 | { |
||
| 155 | return config('model-status.model_primary_key_attribute') ?? 'model_id'; |
||
| 156 | } |
||
| 157 | |||
| 158 | protected function getStatusModelClassName(): string |
||
| 159 | { |
||
| 160 | return config('model-status.status_model'); |
||
| 161 | } |
||
| 162 | |||
| 163 | protected function getStatusModelType(): string |
||
| 164 | { |
||
| 165 | return array_search(static::class, Relation::morphMap()) ?: static::class; |
||
| 166 | } |
||
| 167 | } |
||
| 168 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.