forked from riasvdv/statamic-redirect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathError.php
More file actions
78 lines (63 loc) · 1.99 KB
/
Error.php
File metadata and controls
78 lines (63 loc) · 1.99 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace Rias\StatamicRedirect\Data;
use Illuminate\Database\Eloquent\JsonEncodingException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Error extends Model
{
protected $guarded = [];
protected $casts = [
'handled' => 'boolean',
'lastSeenAt' => 'timestamp',
'hitsCount' => 'int',
];
public $timestamps = false;
public function getConnectionName()
{
if (config('statamic.redirect.connection') === 'redirect') {
return 'redirect-sqlite';
}
if (config('statamic.redirect.connection') !== null) {
return config('statamic.redirect.connection');
}
if (config('statamic.redirect.error_connection') === 'default') {
return config('database.default');
}
return config('statamic.redirect.error_connection', 'redirect-sqlite');
}
protected static function booted()
{
self::saving(function ($model) {
$model->url_md5 = md5($model->url);
});
self::deleting(function ($error) {
$error->hits()->delete();
});
}
public function hits(): HasMany
{
return $this->hasMany(Hit::class, 'error_id');
}
public function addHit(int $timestamp, array $data = []): ?Hit
{
$this->update([
'lastSeenAt' => $timestamp,
'hitsCount' => $this->hitsCount + 1,
]);
if (config('statamic.redirect.log_hits')) {
try {
return $this->hits()->create([
'timestamp' => $timestamp,
'data' => $data,
]);
} catch (JsonEncodingException $e) {
// Malformed data or urls are probably from malicious requests
}
}
return null;
}
public static function findByUrl(string $url): ?self
{
return self::where('url_md5', md5($url))->where('url', $url)->first();
}
}