ctrlpanel/app/Models/Nest.php
Shift 70208d2157
Apply Laravel coding style
Shift automatically applies the Laravel coding style - which uses the PSR-12 coding style as a base with some minor additions.

You may customize the code style applied by configuring [Pint](https://laravel.com/docs/pint), [PHP CS Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer), or [PHP CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) for your project root.

For more information on customizing the code style applied by Shift, [watch this short video](https://laravelshift.com/videos/shift-code-style).
2023-01-05 17:01:42 +00:00

82 lines
1.9 KiB
PHP

<?php
namespace App\Models;
use App\Classes\Pterodactyl;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Nest extends Model
{
use HasFactory;
public $incrementing = false;
public $fillable = [
'id',
'name',
'description',
'disabled',
];
public static function boot()
{
parent::boot(); // TODO: Change the autogenerated stub
static::deleting(function (Nest $nest) {
$nest->eggs()->each(function (Egg $egg) {
$egg->delete();
});
});
}
public static function syncNests()
{
$nests = Pterodactyl::getNests();
//map response
$nests = array_map(function ($nest) {
return [
'id' => $nest['attributes']['id'],
'name' => $nest['attributes']['name'],
'description' => $nest['attributes']['description'],
];
}, $nests);
foreach ($nests as $nest) {
self::query()->updateOrCreate([
'id' => $nest['id'],
], [
'name' => $nest['name'],
'description' => $nest['description'],
'disabled' => false,
]);
}
self::removeDeletedNests($nests);
}
/**
* @description remove nests that have been deleted on pterodactyl
*
* @param array $nests
*/
private static function removeDeletedNests(array $nests): void
{
$ids = array_map(function ($data) {
return $data['id'];
}, $nests);
self::all()->each(function (Nest $nest) use ($ids) {
if (! in_array($nest->id, $ids)) {
$nest->delete();
}
});
}
public function eggs()
{
return $this->hasMany(Egg::class);
}
}