2021-06-05 09:26:32 +00:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 16:38:03 +00:00
|
|
|
namespace App\Models\Pterodactyl;
|
2021-06-05 09:26:32 +00:00
|
|
|
|
2023-02-03 16:38:03 +00:00
|
|
|
use App\Classes\PterodactylClient;
|
2021-06-05 09:26:32 +00:00
|
|
|
use Exception;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2021-07-06 19:38:13 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2021-06-05 09:26:32 +00:00
|
|
|
|
|
|
|
class Node extends Model
|
|
|
|
{
|
|
|
|
use HasFactory;
|
|
|
|
|
|
|
|
public $incrementing = false;
|
|
|
|
|
|
|
|
public $guarded = [];
|
|
|
|
|
2021-11-07 11:07:30 +00:00
|
|
|
public static function boot()
|
2021-06-05 09:26:32 +00:00
|
|
|
{
|
2021-11-07 11:07:30 +00:00
|
|
|
parent::boot(); // TODO: Change the autogenerated stub
|
|
|
|
|
|
|
|
static::deleting(function (Node $node) {
|
|
|
|
$node->products()->detach();
|
|
|
|
});
|
2021-06-05 09:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2021-11-07 11:07:30 +00:00
|
|
|
public static function syncNodes()
|
|
|
|
{
|
2021-06-05 09:26:32 +00:00
|
|
|
Location::syncLocations();
|
2023-02-03 16:38:03 +00:00
|
|
|
$client = app(PterodactylClient::class);
|
|
|
|
$nodes = $client->getNodes();
|
2021-06-05 09:26:32 +00:00
|
|
|
|
2021-11-07 11:07:30 +00:00
|
|
|
//map response
|
|
|
|
$nodes = array_map(function ($node) {
|
2023-01-05 17:01:42 +00:00
|
|
|
return [
|
|
|
|
'id' => $node['attributes']['id'],
|
2021-06-05 09:26:32 +00:00
|
|
|
'location_id' => $node['attributes']['location_id'],
|
2023-01-05 17:01:42 +00:00
|
|
|
'name' => $node['attributes']['name'],
|
2021-06-05 09:26:32 +00:00
|
|
|
'description' => $node['attributes']['description'],
|
2023-01-05 17:01:42 +00:00
|
|
|
];
|
2021-06-05 09:26:32 +00:00
|
|
|
}, $nodes);
|
|
|
|
|
2021-11-07 11:07:30 +00:00
|
|
|
//update or create
|
2021-06-05 09:26:32 +00:00
|
|
|
foreach ($nodes as $node) {
|
2021-11-07 11:07:30 +00:00
|
|
|
self::query()->updateOrCreate(
|
|
|
|
[
|
2023-01-05 17:01:42 +00:00
|
|
|
'id' => $node['id'],
|
2021-11-07 11:07:30 +00:00
|
|
|
],
|
|
|
|
[
|
2023-01-05 17:01:42 +00:00
|
|
|
'name' => $node['name'],
|
2021-11-07 11:07:30 +00:00
|
|
|
'description' => $node['description'],
|
|
|
|
'location_id' => $node['location_id'],
|
2023-01-05 17:01:42 +00:00
|
|
|
'disabled' => false,
|
2021-11-07 11:07:30 +00:00
|
|
|
]);
|
2021-06-05 09:26:32 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 11:07:30 +00:00
|
|
|
self::removeDeletedNodes($nodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description remove nodes that have been deleted on pterodactyl
|
2023-01-05 17:01:42 +00:00
|
|
|
*
|
|
|
|
* @param array $nodes
|
2021-11-07 11:07:30 +00:00
|
|
|
*/
|
|
|
|
private static function removeDeletedNodes(array $nodes): void
|
|
|
|
{
|
|
|
|
$ids = array_map(function ($data) {
|
|
|
|
return $data['id'];
|
|
|
|
}, $nodes);
|
|
|
|
|
|
|
|
self::all()->each(function (Node $node) use ($ids) {
|
2023-01-05 17:01:42 +00:00
|
|
|
if (! in_array($node->id, $ids)) {
|
|
|
|
$node->delete();
|
|
|
|
}
|
2021-11-07 11:07:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return BelongsTo
|
|
|
|
*/
|
|
|
|
public function location(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Location::class);
|
2021-06-05 09:26:32 +00:00
|
|
|
}
|
2021-07-06 19:38:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return BelongsToMany
|
|
|
|
*/
|
|
|
|
public function products()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Product::class);
|
|
|
|
}
|
2021-06-05 09:26:32 +00:00
|
|
|
}
|