RestrictedLinkOpenAction   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 72
ccs 28
cts 28
cp 1
rs 10
wmc 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 7 2
A fillBrowserFingerPrint() 0 4 1
A verified() 0 3 2
A checkBrowserFingerPrint() 0 3 1
A link() 0 3 1
A makeBrowserFingerPrint() 0 3 1
A accessExpired() 0 7 2
A newFactory() 0 3 1
A getTable() 0 3 1
A viewer() 0 3 1
1
<?php
2
3
namespace LinkRestrictedAccess\Models;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Factories\HasFactory;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\BelongsTo;
9
use Illuminate\Database\Eloquent\Relations\MorphTo;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Str;
12
use JsonFieldCast\Casts\SimpleJsonField;
13
use LinkRestrictedAccess\Database\Factories\RestrictedLinkOpenActionFactory;
14
15
/**
16
 * @property \JsonFieldCast\Json\SimpleJsonField $verification_result
17
 * @property \JsonFieldCast\Json\SimpleJsonField $meta
18
 */
19
class RestrictedLinkOpenAction extends Model
20
{
21
    use HasFactory;
22
23
    protected $guarded = [];
24
25
    protected $casts = [
26
        'viewed_at'           => 'datetime',
27
        'verification_result' => SimpleJsonField::class,
28
        'meta'                => SimpleJsonField::class,
29
    ];
30
31 7
    public function getTable(): string
32
    {
33 7
        return config('restricted-access.tables.opens');
34
    }
35
36 7
    protected static function boot(): void
37
    {
38 7
        parent::boot();
39
40 7
        static::saving(function ($model) {
41 7
            if (!$model->uuid) {
42 7
                $model->uuid = (string)Str::uuid();
43
            }
44 7
        });
45
    }
46
47 1
    public function link(): BelongsTo
48
    {
49 1
        return $this->belongsTo(\LinkRestrictedAccess\RestrictedAccess::restrictedLinkModel(), 'link_id', 'id');
50
    }
51
52 1
    public function viewer(): MorphTo
53
    {
54 1
        return $this->morphTo('viewer');
55
    }
56
57 3
    public function accessExpired(): bool
58
    {
59 3
        if ($this->viewed_at) {
0 ignored issues
show
Bug introduced by
The property viewed_at does not seem to exist on LinkRestrictedAccess\Mod...estrictedLinkOpenAction. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
60 3
            return Carbon::now()->subHours(24)->greaterThan($this->viewed_at);
61
        }
62
63 1
        return true;
64
    }
65
66 5
    protected function makeBrowserFingerPrint(Request $request): string
67
    {
68 5
        return md5($request->ip() . '_' . $request->userAgent());
69
    }
70
71 5
    public function fillBrowserFingerPrint(Request $request)
72
    {
73 5
        return $this->fill([
74 5
            'browser_fingerprint' => $this->makeBrowserFingerPrint($request),
75 5
        ]);
76
    }
77
78 2
    public function checkBrowserFingerPrint(Request $request): bool
79
    {
80 2
        return $this->browser_fingerprint == $this->makeBrowserFingerPrint($request);
0 ignored issues
show
Bug introduced by
The property browser_fingerprint does not seem to exist on LinkRestrictedAccess\Mod...estrictedLinkOpenAction. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
81
    }
82
83 2
    public function verified(Request $request): bool
84
    {
85 2
        return $this->checkBrowserFingerPrint($request) && !$this->accessExpired();
86
    }
87
88 4
    protected static function newFactory(): RestrictedLinkOpenActionFactory
89
    {
90 4
        return RestrictedLinkOpenActionFactory::new();
91
    }
92
}
93