Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| LogEntry | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
4 | |
100.00% |
1 / 1 |
| filter | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Engelsystem\Models; |
| 6 | |
| 7 | use Carbon\Carbon; |
| 8 | use Engelsystem\Models\User\User; |
| 9 | use Engelsystem\Models\User\UsesUserModel; |
| 10 | use Illuminate\Database\Eloquent\Builder; |
| 11 | use Illuminate\Database\Eloquent\Collection; |
| 12 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
| 13 | use Illuminate\Database\Query\Builder as QueryBuilder; |
| 14 | use Illuminate\Support\Collection as SupportCollection; |
| 15 | |
| 16 | /** |
| 17 | * @property int $id |
| 18 | * @property int|null $user_id |
| 19 | * @property string $level |
| 20 | * @property string $message |
| 21 | * @property Carbon|null $created_at |
| 22 | * |
| 23 | * @property-read User|null $user |
| 24 | * |
| 25 | * @method static QueryBuilder|LogEntry[] whereId($value) |
| 26 | * @method static QueryBuilder|LogEntry[] whereLevel($value) |
| 27 | * @method static QueryBuilder|LogEntry[] whereMessage($value) |
| 28 | * @method static QueryBuilder|LogEntry[] whereCreatedAt($value) |
| 29 | */ |
| 30 | class LogEntry extends BaseModel |
| 31 | { |
| 32 | use UsesUserModel; |
| 33 | use HasFactory; |
| 34 | |
| 35 | /** @var bool enable timestamps for created_at */ |
| 36 | public $timestamps = true; // phpcs:ignore |
| 37 | |
| 38 | /** @var null Disable updated_at */ |
| 39 | public const UPDATED_AT = null; |
| 40 | |
| 41 | /** @var array Default attributes */ |
| 42 | protected $attributes = [ // phpcs:ignore |
| 43 | 'user_id' => null, |
| 44 | ]; |
| 45 | |
| 46 | /** @var array<string, string> */ |
| 47 | protected $casts = [ // phpcs:ignore |
| 48 | 'user_id' => 'integer', |
| 49 | ]; |
| 50 | |
| 51 | /** |
| 52 | * The attributes that are mass assignable. |
| 53 | */ |
| 54 | protected $fillable = [ // phpcs:ignore |
| 55 | 'level', |
| 56 | 'message', |
| 57 | 'user_id', |
| 58 | ]; |
| 59 | |
| 60 | /** |
| 61 | * @return Builder[]|Collection|SupportCollection|LogEntry[] |
| 62 | */ |
| 63 | public static function filter( |
| 64 | ?string $keyword = null, |
| 65 | ?int $userId = null, |
| 66 | ?string $level = null |
| 67 | ): array | Collection | SupportCollection { |
| 68 | $query = self::with(['user', 'user.personalData', 'user.state']) |
| 69 | ->orderByDesc('created_at') |
| 70 | ->orderByDesc('id') |
| 71 | ->limit(10000); |
| 72 | |
| 73 | if (!empty($userId)) { |
| 74 | $query->where(function (Builder $query) use ($userId): void { |
| 75 | $user = User::findOrFail($userId); |
| 76 | $query->where('user_id', $userId) |
| 77 | ->orWhere('message', 'like', '%' . $user->name . ' (' . $userId . ')%'); |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | if (!empty($level)) { |
| 82 | $query->where('level', '=', $level); |
| 83 | } |
| 84 | |
| 85 | if (!empty($keyword)) { |
| 86 | $query->where('message', 'LIKE', '%' . $keyword . '%'); |
| 87 | } |
| 88 | |
| 89 | return $query->get(); |
| 90 | } |
| 91 | } |