-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTermsOfUseVersion.php
More file actions
54 lines (43 loc) · 1.24 KB
/
TermsOfUseVersion.php
File metadata and controls
54 lines (43 loc) · 1.24 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
<?php
namespace App;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* Bug: T401165 https://phabricator.wikimedia.org/T401165
* Be mindful that multiple ToU versions may exist over time,
* but only one should be active at a time.
*/
class TermsOfUseVersion extends Model {
use HasFactory;
protected $table = 'tou_versions';
const FIELDS = [
'version',
'active',
];
protected $fillable = self::FIELDS;
protected $visible = self::FIELDS;
protected $casts = [
'version' => 'string',
'active' => 'boolean',
];
/**
* Get the active ToU version.
*/
public static function latestActiveVersion(): ?self {
return self::query()->where('active', true)->first();
}
/**
* Ensure only one ToU version remains active after any save operation.
*/
protected static function booted(): void {
static::saving(function (self $model): void {
if (!$model->active) {
return;
}
self::query()
->where('version', '!=', $model->version)
->where('active', true)
->update(['active' => false]);
});
}
}