summaryrefslogtreecommitdiff
path: root/src/Models/AccessLog.php
blob: a0bdf8efc1377ff7ad413cb655fc5ce0f188eaf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php

namespace Lightscale\LaralightAccessLog\Models;

use Lightscale\LaralightAccessLog\Database\Factories\AccessLogFactory;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Prunable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Builder;

class AccessLog extends Model
{
    use HasFactory, Prunable;

    const UPDATED_AT = null;

    protected $guarded = [
        'user_id'
    ];

    public function __construct(array $attributes = [])
    {
        $this->setTable(config('access_log.table_name'));

        parent::__construct($attributes);
    }

    protected function casts(): array
    {
        return [
            'properties' => 'collection',
        ];
    }

    public static function newFactory()
    {
        return AccessLogFactory::new();
    }

    public function user(): BelongsTo
    {
        return $this->belongsTo(config('access_log.user_model'));
    }

    public function prunable(): Builder
    {
        $days = config('access_log.max_age_days');
        if ($days === null) {
            return static::whereRaw('1 = 0');
        }
        else return static::where(static::CREATED_AT, '<=', now()->subDays($days));
    }
}