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 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;
|
2023-02-03 16:38:03 +00:00
|
|
|
use App\Models\Pterodactyl\Nest;
|
2021-06-05 09:26:32 +00:00
|
|
|
|
|
|
|
class Egg extends Model
|
|
|
|
{
|
|
|
|
use HasFactory;
|
|
|
|
|
|
|
|
public $incrementing = false;
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
'id',
|
|
|
|
'nest_id',
|
|
|
|
'name',
|
|
|
|
'description',
|
|
|
|
'docker_image',
|
|
|
|
'startup',
|
|
|
|
'environment',
|
2021-11-07 11:07:30 +00:00
|
|
|
'updated_at',
|
2021-06-05 09:26:32 +00:00
|
|
|
];
|
|
|
|
|
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
|
2021-06-05 09:26:32 +00:00
|
|
|
|
2021-11-07 11:07:30 +00:00
|
|
|
static::deleting(function (Egg $egg) {
|
|
|
|
$egg->products()->detach();
|
|
|
|
});
|
2021-06-05 09:26:32 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 19:38:13 +00:00
|
|
|
public static function syncEggs()
|
|
|
|
{
|
2021-11-07 11:07:30 +00:00
|
|
|
Nest::syncNests();
|
2023-02-03 16:38:03 +00:00
|
|
|
$client = app(PterodactylClient::class);
|
|
|
|
Nest::all()->each(function (Nest $nest) use ($client) {
|
|
|
|
$eggs = $client->getEggs($nest);
|
2021-06-05 09:26:32 +00:00
|
|
|
|
2021-07-06 19:38:13 +00:00
|
|
|
foreach ($eggs as $egg) {
|
2021-06-05 09:26:32 +00:00
|
|
|
$array = [];
|
|
|
|
$environment = [];
|
|
|
|
|
|
|
|
$array['id'] = $egg['attributes']['id'];
|
|
|
|
$array['nest_id'] = $egg['attributes']['nest'];
|
|
|
|
$array['name'] = $egg['attributes']['name'];
|
|
|
|
$array['description'] = $egg['attributes']['description'];
|
|
|
|
$array['docker_image'] = $egg['attributes']['docker_image'];
|
|
|
|
$array['startup'] = $egg['attributes']['startup'];
|
2021-11-07 11:07:30 +00:00
|
|
|
$array['updated_at'] = now();
|
2021-06-05 09:26:32 +00:00
|
|
|
|
|
|
|
//get environment variables
|
2021-07-06 19:38:13 +00:00
|
|
|
foreach ($egg['attributes']['relationships']['variables']['data'] as $variable) {
|
2021-06-05 09:26:32 +00:00
|
|
|
$environment[$variable['attributes']['env_variable']] = $variable['attributes']['default_value'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$array['environment'] = json_encode([$environment]);
|
|
|
|
|
2021-11-07 11:07:30 +00:00
|
|
|
self::query()->updateOrCreate([
|
2023-01-05 17:01:42 +00:00
|
|
|
'id' => $array['id'],
|
|
|
|
], array_diff_key($array, array_flip(['id']))
|
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::removeDeletedEggs($nest, $eggs);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description remove eggs that have been deleted on pterodactyl
|
2023-01-05 17:01:42 +00:00
|
|
|
*
|
|
|
|
* @param Nest $nest
|
|
|
|
* @param array $eggs
|
2021-11-07 11:07:30 +00:00
|
|
|
*/
|
|
|
|
private static function removeDeletedEggs(Nest $nest, array $eggs): void
|
|
|
|
{
|
|
|
|
$ids = array_map(function ($data) {
|
|
|
|
return $data['attributes']['id'];
|
|
|
|
}, $eggs);
|
|
|
|
|
|
|
|
$nest->eggs()->each(function (Egg $egg) use ($ids) {
|
2023-01-05 17:01:42 +00:00
|
|
|
if (! in_array($egg->id, $ids)) {
|
|
|
|
$egg->delete();
|
|
|
|
}
|
2021-06-05 09:26:32 +00:00
|
|
|
});
|
2021-07-06 19:38:13 +00:00
|
|
|
}
|
2021-06-05 09:26:32 +00:00
|
|
|
|
2021-11-07 11:07:30 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getEnvironmentVariables()
|
|
|
|
{
|
|
|
|
$array = [];
|
|
|
|
|
|
|
|
foreach (json_decode($this->environment) as $variable) {
|
|
|
|
foreach ($variable as $key => $value) {
|
|
|
|
$array[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2021-07-06 19:38:13 +00:00
|
|
|
/**
|
|
|
|
* @return BelongsTo
|
|
|
|
*/
|
|
|
|
public function nest()
|
|
|
|
{
|
2021-11-07 11:07:30 +00:00
|
|
|
return $this->belongsTo(Nest::class);
|
2021-07-06 19:38:13 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|