Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
LogEntry
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
1 / 1
 filter
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Models;
6
7use Carbon\Carbon;
8use Engelsystem\Models\User\User;
9use Engelsystem\Models\User\UsesUserModel;
10use Illuminate\Database\Eloquent\Builder;
11use Illuminate\Database\Eloquent\Collection;
12use Illuminate\Database\Eloquent\Factories\HasFactory;
13use Illuminate\Database\Query\Builder as QueryBuilder;
14use 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 */
30class 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(?string $keyword = null, ?int $userId = null): array|Collection|SupportCollection
64    {
65        $query = self::with(['user', 'user.personalData', 'user.state'])
66            ->orderByDesc('created_at')
67            ->orderByDesc('id')
68            ->limit(10000);
69
70        if (!empty($userId)) {
71            $query->where(function (Builder $query) use ($userId): void {
72                $user = User::findOrFail($userId);
73                $query->where('user_id', $userId)
74                    ->orWhere('message', 'like', '%' . $user->name . ' (' . $userId . ')%');
75            });
76        }
77
78        if (!empty($keyword)) {
79            $query
80                ->where(function (Builder $query) use ($keyword): void {
81                    $query->where('level', '=', $keyword)
82                        ->orWhere('message', 'LIKE', '%' . $keyword . '%');
83                });
84        }
85
86        return $query->get();
87    }
88}