-
-
Notifications
You must be signed in to change notification settings - Fork 269
Expand file tree
/
Copy pathAllocation.php
More file actions
141 lines (126 loc) · 4.35 KB
/
Allocation.php
File metadata and controls
141 lines (126 loc) · 4.35 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace App\Models;
use App\Exceptions\Service\Allocation\ServerUsingAllocationException;
use App\Traits\HasValidation;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Carbon;
/**
* App\Models\Allocation.
*
* @property int $id
* @property int $node_id
* @property string $ip
* @property int $port
* @property int|null $server_id
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property string|null $ip_alias
* @property string|null $notes
* @property bool $is_locked
* @property-read string $address
* @property-read string $alias
* @property-read bool $has_alias
* @property-read Node $node
* @property-read Server|null $server
*
* @method static \Database\Factories\AllocationFactory factory($count = null, $state = [])
* @method static Builder<static>|Allocation newModelQuery()
* @method static Builder<static>|Allocation newQuery()
* @method static Builder<static>|Allocation query()
* @method static Builder<static>|Allocation whereCreatedAt($value)
* @method static Builder<static>|Allocation whereId($value)
* @method static Builder<static>|Allocation whereIp($value)
* @method static Builder<static>|Allocation whereIpAlias($value)
* @method static Builder<static>|Allocation whereIsLocked($value)
* @method static Builder<static>|Allocation whereNodeId($value)
* @method static Builder<static>|Allocation whereNotes($value)
* @method static Builder<static>|Allocation wherePort($value)
* @method static Builder<static>|Allocation whereServerId($value)
* @method static Builder<static>|Allocation whereUpdatedAt($value)
*/
class Allocation extends Model
{
use HasFactory;
use HasValidation;
/**
* The resource name for this model when it is transformed into an
* API representation using fractal. Also used as name for api key permissions.
*/
public const RESOURCE_NAME = 'allocation';
protected $attributes = [
'is_locked' => false,
];
/**
* Fields that are not mass assignable.
*/
protected $guarded = ['id', 'created_at', 'updated_at'];
/** @var array<array-key, string[]> */
public static array $validationRules = [
'node_id' => ['required', 'exists:nodes,id'],
'ip' => ['required', 'ip'],
'port' => ['required', 'numeric', 'between:1024,65535'],
'ip_alias' => ['nullable', 'string'],
'server_id' => ['nullable', 'exists:servers,id'],
'notes' => ['nullable', 'string', 'max:256'],
'is_locked' => ['boolean'],
];
protected static function booted(): void
{
static::updating(function (self $allocation) {
if (is_null($allocation->server_id)) {
$allocation->is_locked = false;
}
});
static::deleting(function (self $allocation) {
throw_if($allocation->server_id, new ServerUsingAllocationException(trans('exceptions.allocations.server_using')));
});
}
protected function casts(): array
{
return [
'node_id' => 'integer',
'port' => 'integer',
'server_id' => 'integer',
'is_locked' => 'bool',
];
}
/**
* Accessor to automatically provide the IP alias if defined.
*/
public function getAliasAttribute(?string $value): string
{
return (is_null($this->ip_alias)) ? $this->ip : $this->ip_alias;
}
/**
* Accessor to quickly determine if this allocation has an alias.
*/
public function getHasAliasAttribute(?string $value): bool
{
return !is_null($this->ip_alias);
}
/** @return Attribute<string, never> */
protected function address(): Attribute
{
return Attribute::make(
get: fn () => (is_ipv6($this->alias) ? "[$this->alias]" : $this->alias) . ":$this->port",
);
}
/**
* Gets information for the server associated with this allocation.
*/
public function server(): BelongsTo
{
return $this->belongsTo(Server::class);
}
/**
* Return the Node model associated with this allocation.
*/
public function node(): BelongsTo
{
return $this->belongsTo(Node::class);
}
}