ctrlpanel/app/Models/Server.php

150 lines
3.2 KiB
PHP
Raw Permalink Normal View History

2021-06-05 09:26:32 +00:00
<?php
namespace App\Models;
use App\Classes\Pterodactyl;
2021-06-05 09:26:32 +00:00
use Exception;
use GuzzleHttp\Promise\PromiseInterface;
use Hidehalo\Nanoid\Client;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Http\Client\Response;
2023-01-05 18:33:46 +00:00
use Spatie\Activitylog\LogOptions;
2021-06-05 09:26:32 +00:00
use Spatie\Activitylog\Traits\LogsActivity;
/**
* Class Server
*/
class Server extends Model
{
use HasFactory;
use LogsActivity;
2023-01-05 18:33:46 +00:00
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
-> logOnlyDirty()
-> logOnly(['*'])
-> dontSubmitEmptyLogs();
2023-01-05 18:33:46 +00:00
}
2021-06-05 09:26:32 +00:00
/**
* @var bool
*/
public $incrementing = false;
/**
* @var string[]
*/
2021-06-06 20:04:46 +00:00
protected static $ignoreChangedAttributes = ['pterodactyl_id', 'identifier', 'updated_at'];
2021-06-05 09:26:32 +00:00
/**
* @var string[]
*/
protected static $logAttributes = ['name', 'description'];
/**
* @var string[]
*/
protected $fillable = [
'name',
'description',
'suspended',
'identifier',
'product_id',
'pterodactyl_id',
2021-06-05 09:26:32 +00:00
];
/**
* @var string[]
*/
protected $casts = [
'suspended' => 'datetime',
2021-06-05 09:26:32 +00:00
];
2021-06-06 20:04:46 +00:00
public static function boot()
{
2021-06-05 09:26:32 +00:00
parent::boot();
2021-06-06 20:04:46 +00:00
static::creating(function (Server $server) {
2021-06-05 09:26:32 +00:00
$client = new Client();
$server->{$server->getKeyName()} = $client->generateId($size = 21);
});
2021-06-06 20:04:46 +00:00
static::deleting(function (Server $server) {
$response = Pterodactyl::client()->delete("/application/servers/{$server->pterodactyl_id}");
if ($response->failed() && ! is_null($server->pterodactyl_id)) {
//only return error when it's not a 404 error
if ($response['errors'][0]['status'] != '404') {
throw new Exception($response['errors'][0]['code']);
}
}
2021-06-05 09:26:32 +00:00
});
}
/**
* @return bool
*/
2021-06-06 20:04:46 +00:00
public function isSuspended()
{
return ! is_null($this->suspended);
2021-06-05 09:26:32 +00:00
}
/**
* @return PromiseInterface|Response
*/
2021-06-06 20:04:46 +00:00
public function getPterodactylServer()
{
return Pterodactyl::client()->get("/application/servers/{$this->pterodactyl_id}");
2021-06-05 09:26:32 +00:00
}
/**
* @throws Exception
*/
2021-06-06 20:04:46 +00:00
public function suspend()
{
$response = Pterodactyl::suspendServer($this);
2021-06-05 09:26:32 +00:00
if ($response->successful()) {
$this->update([
'suspended' => now(),
2021-06-05 09:26:32 +00:00
]);
}
return $this;
}
/**
* @throws Exception
*/
public function unSuspend()
{
$response = Pterodactyl::unSuspendServer($this);
2021-06-05 09:26:32 +00:00
if ($response->successful()) {
$this->update([
'suspended' => null,
2021-06-05 09:26:32 +00:00
]);
}
return $this;
}
/**
* @return HasOne
*/
2021-06-06 20:04:46 +00:00
public function product()
{
return $this->hasOne(Product::class, 'id', 'product_id');
2021-06-05 09:26:32 +00:00
}
/**
* @return BelongsTo
*/
2021-06-06 20:04:46 +00:00
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
2021-06-05 09:26:32 +00:00
}
}