cybercog /
laravel-eloquent-flag
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | /* |
||||||
| 4 | * This file is part of Laravel Eloquent Flag. |
||||||
| 5 | * |
||||||
| 6 | * (c) Anton Komarev <[email protected]> |
||||||
| 7 | * |
||||||
| 8 | * For the full copyright and license information, please view the LICENSE |
||||||
| 9 | * file that was distributed with this source code. |
||||||
| 10 | */ |
||||||
| 11 | |||||||
| 12 | declare(strict_types=1); |
||||||
| 13 | |||||||
| 14 | namespace Cog\Flag\Traits\Classic; |
||||||
| 15 | |||||||
| 16 | use Cog\Flag\Scopes\Classic\KeptFlagScope; |
||||||
| 17 | use Illuminate\Database\Eloquent\Builder; |
||||||
| 18 | use Illuminate\Support\Facades\Date; |
||||||
| 19 | |||||||
| 20 | trait HasKeptFlagHelpers |
||||||
| 21 | { |
||||||
| 22 | public function initializeHasKeptFlagHelpers(): void |
||||||
| 23 | { |
||||||
| 24 | $this->casts['is_kept'] = 'boolean'; |
||||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||||
| 25 | } |
||||||
| 26 | |||||||
| 27 | public function isKept(): bool |
||||||
| 28 | { |
||||||
| 29 | return $this->getAttributeValue('is_kept'); |
||||||
|
0 ignored issues
–
show
It seems like
getAttributeValue() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 30 | } |
||||||
| 31 | |||||||
| 32 | public function isNotKept(): bool |
||||||
| 33 | { |
||||||
| 34 | return !$this->isKept(); |
||||||
| 35 | } |
||||||
| 36 | |||||||
| 37 | public function keep(): void |
||||||
| 38 | { |
||||||
| 39 | $this->setAttribute('is_kept', true); |
||||||
|
0 ignored issues
–
show
It seems like
setAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 40 | $this->save(); |
||||||
|
0 ignored issues
–
show
It seems like
save() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 41 | |||||||
| 42 | $this->fireModelEvent('kept', false); |
||||||
|
0 ignored issues
–
show
It seems like
fireModelEvent() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 43 | } |
||||||
| 44 | |||||||
| 45 | public function undoKeep(): void |
||||||
| 46 | { |
||||||
| 47 | $this->setAttribute('is_kept', false); |
||||||
| 48 | if (property_exists($this, 'setKeptOnUpdate')) { |
||||||
| 49 | $this->setKeptOnUpdate = false; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 50 | } |
||||||
| 51 | $this->save(); |
||||||
| 52 | |||||||
| 53 | $this->fireModelEvent('keptUndone', false); |
||||||
| 54 | } |
||||||
| 55 | |||||||
| 56 | /** |
||||||
| 57 | * Get unkept models that are older than the given number of hours. |
||||||
| 58 | * |
||||||
| 59 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||||||
| 60 | * @param int $hours |
||||||
| 61 | * @return \Illuminate\Database\Eloquent\Builder |
||||||
| 62 | */ |
||||||
| 63 | public function scopeOnlyUnkeptOlderThanHours(Builder $builder, $hours) |
||||||
| 64 | { |
||||||
| 65 | return $builder |
||||||
| 66 | ->withoutGlobalScope(KeptFlagScope::class) |
||||||
| 67 | ->where('is_kept', 0) |
||||||
| 68 | ->where(static::getCreatedAtColumn(), '<=', Date::now()->subHours($hours)->toDateTimeString()); |
||||||
| 69 | } |
||||||
| 70 | } |
||||||
| 71 |