2015-12-07 00:47:19 -05:00
|
|
|
<?php
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2024-03-12 22:39:16 -04:00
|
|
|
namespace App\Models;
|
2015-12-07 00:47:19 -05:00
|
|
|
|
2025-09-24 13:34:19 +02:00
|
|
|
use App\Exceptions\Service\Allocation\ServerUsingAllocationException;
|
|
|
|
|
use App\Traits\HasValidation;
|
2025-09-08 13:12:33 -04:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2024-03-26 20:52:32 -04:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2025-01-30 16:39:00 -05:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2025-09-24 13:34:19 +02:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2022-10-14 10:59:20 -06:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2026-03-17 09:09:01 +01:00
|
|
|
use Illuminate\Support\Carbon;
|
2022-10-14 10:59:20 -06:00
|
|
|
|
2019-11-16 13:33:01 -08:00
|
|
|
/**
|
2024-03-12 22:39:16 -04:00
|
|
|
* App\Models\Allocation.
|
2022-05-07 15:05:28 -04:00
|
|
|
*
|
2021-01-27 20:52:11 -08:00
|
|
|
* @property int $id
|
|
|
|
|
* @property int $node_id
|
|
|
|
|
* @property string $ip
|
|
|
|
|
* @property int $port
|
|
|
|
|
* @property int|null $server_id
|
2025-09-08 13:12:33 -04:00
|
|
|
* @property Carbon|null $created_at
|
|
|
|
|
* @property Carbon|null $updated_at
|
2026-03-17 09:09:01 +01:00
|
|
|
* @property string|null $ip_alias
|
|
|
|
|
* @property string|null $notes
|
2025-11-08 21:54:41 +01:00
|
|
|
* @property bool $is_locked
|
2026-03-17 09:09:01 +01:00
|
|
|
* @property-read string $address
|
|
|
|
|
* @property-read string $alias
|
|
|
|
|
* @property-read bool $has_alias
|
|
|
|
|
* @property-read Node $node
|
|
|
|
|
* @property-read Server|null $server
|
2022-05-07 15:05:28 -04:00
|
|
|
*
|
2026-03-17 09:09:01 +01:00
|
|
|
* @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)
|
2019-11-16 13:33:01 -08:00
|
|
|
*/
|
2020-04-03 23:22:35 -07:00
|
|
|
class Allocation extends Model
|
2015-12-07 00:47:19 -05:00
|
|
|
{
|
2025-01-30 16:39:00 -05:00
|
|
|
use HasFactory;
|
|
|
|
|
use HasValidation;
|
|
|
|
|
|
2018-01-25 21:26:06 -06:00
|
|
|
/**
|
|
|
|
|
* The resource name for this model when it is transformed into an
|
2024-11-06 09:09:10 +01:00
|
|
|
* API representation using fractal. Also used as name for api key permissions.
|
2018-01-25 21:26:06 -06:00
|
|
|
*/
|
2021-01-23 12:33:34 -08:00
|
|
|
public const RESOURCE_NAME = 'allocation';
|
2018-01-25 21:26:06 -06:00
|
|
|
|
2025-11-08 21:54:41 +01:00
|
|
|
protected $attributes = [
|
|
|
|
|
'is_locked' => false,
|
|
|
|
|
];
|
|
|
|
|
|
2016-01-10 00:38:16 -05:00
|
|
|
/**
|
|
|
|
|
* Fields that are not mass assignable.
|
|
|
|
|
*/
|
|
|
|
|
protected $guarded = ['id', 'created_at', 'updated_at'];
|
|
|
|
|
|
2025-03-08 19:56:06 -05:00
|
|
|
/** @var array<array-key, string[]> */
|
2022-10-14 10:59:20 -06:00
|
|
|
public static array $validationRules = [
|
2025-03-03 14:41:19 -05:00
|
|
|
'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'],
|
2025-11-08 21:54:41 +01:00
|
|
|
'is_locked' => ['boolean'],
|
2017-08-05 21:10:32 -05:00
|
|
|
];
|
2017-02-05 19:19:46 -05:00
|
|
|
|
2024-03-19 15:48:11 -04:00
|
|
|
protected static function booted(): void
|
|
|
|
|
{
|
2025-11-08 21:54:41 +01:00
|
|
|
static::updating(function (self $allocation) {
|
|
|
|
|
if (is_null($allocation->server_id)) {
|
|
|
|
|
$allocation->is_locked = false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2024-03-19 15:48:11 -04:00
|
|
|
static::deleting(function (self $allocation) {
|
|
|
|
|
throw_if($allocation->server_id, new ServerUsingAllocationException(trans('exceptions.allocations.server_using')));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-19 21:08:49 -04:00
|
|
|
protected function casts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'node_id' => 'integer',
|
|
|
|
|
'port' => 'integer',
|
|
|
|
|
'server_id' => 'integer',
|
2025-11-08 21:54:41 +01:00
|
|
|
'is_locked' => 'bool',
|
2024-03-19 21:08:49 -04:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-20 21:32:57 -05:00
|
|
|
/**
|
2017-08-05 21:10:32 -05:00
|
|
|
* Accessor to automatically provide the IP alias if defined.
|
|
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function getAliasAttribute(?string $value): string
|
2017-08-05 21:10:32 -05:00
|
|
|
{
|
|
|
|
|
return (is_null($this->ip_alias)) ? $this->ip : $this->ip_alias;
|
|
|
|
|
}
|
2017-02-09 17:43:54 -05:00
|
|
|
|
2017-08-05 21:10:32 -05:00
|
|
|
/**
|
|
|
|
|
* Accessor to quickly determine if this allocation has an alias.
|
|
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function getHasAliasAttribute(?string $value): bool
|
2017-08-05 21:10:32 -05:00
|
|
|
{
|
2021-01-23 12:33:34 -08:00
|
|
|
return !is_null($this->ip_alias);
|
2017-08-05 21:10:32 -05:00
|
|
|
}
|
2017-02-17 19:59:40 -05:00
|
|
|
|
2024-06-11 21:00:51 +02:00
|
|
|
/** @return Attribute<string, never> */
|
|
|
|
|
protected function address(): Attribute
|
2024-03-26 20:52:32 -04:00
|
|
|
{
|
|
|
|
|
return Attribute::make(
|
2025-05-09 08:44:18 +02:00
|
|
|
get: fn () => (is_ipv6($this->alias) ? "[$this->alias]" : $this->alias) . ":$this->port",
|
2024-03-26 20:52:32 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-05 21:10:32 -05:00
|
|
|
/**
|
|
|
|
|
* Gets information for the server associated with this allocation.
|
|
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function server(): BelongsTo
|
2017-08-05 21:10:32 -05:00
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Server::class);
|
|
|
|
|
}
|
2018-01-10 23:19:03 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the Node model associated with this allocation.
|
|
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function node(): BelongsTo
|
2018-01-10 23:19:03 -06:00
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Node::class);
|
|
|
|
|
}
|
2015-12-07 00:47:19 -05:00
|
|
|
}
|