2021-06-05 09:26:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use App\Classes\Pterodactyl;
|
|
|
|
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 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();
|
2021-06-05 09:26:32 +00:00
|
|
|
|
2021-07-06 19:38:13 +00:00
|
|
|
Nest::all()->each(function (Nest $nest) {
|
2021-06-05 09:26:32 +00:00
|
|
|
$eggs = Pterodactyl::getEggs($nest);
|
|
|
|
|
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([
|
|
|
|
'id' => $array['id']
|
|
|
|
], array_diff_key($array, array_flip(["id"]))
|
|
|
|
);
|
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
|
|
|
|
* @param Nest $nest
|
|
|
|
* @param array $eggs
|
|
|
|
*/
|
|
|
|
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) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|