ctrlpanel/app/Models/Egg.php

90 lines
2.2 KiB
PHP
Raw Normal View History

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;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
2021-11-06 00:56:57 +00:00
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
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',
];
/**
* @return array
*/
public function getEnvironmentVariables()
{
$array = [];
foreach (json_decode($this->environment) as $variable) {
foreach ($variable as $key => $value) {
2021-06-05 09:26:32 +00:00
$array[$key] = $value;
}
}
return $array;
}
public static function syncEggs()
{
2021-06-05 09:26:32 +00:00
Nest::all()->each(function (Nest $nest) {
2021-06-05 09:26:32 +00:00
$eggs = Pterodactyl::getEggs($nest);
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'];
//get environment variables
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]);
self::firstOrCreate(['id' => $array['id']], $array);
2021-06-05 09:26:32 +00:00
}
});
}
2021-06-05 09:26:32 +00:00
/**
* @return BelongsTo
*/
public function nest()
{
return $this->belongsTo(Nest::class, 'id', 'nest_id');
}
2021-06-05 09:26:32 +00:00
/**
* @return BelongsToMany
*/
public function products()
{
return $this->belongsToMany(Product::class);
2021-06-05 09:26:32 +00:00
}
}