Egg.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Models;
  3. use App\Classes\Pterodactyl;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  8. class Egg extends Model
  9. {
  10. use HasFactory;
  11. public $incrementing = false;
  12. protected $fillable = [
  13. 'id',
  14. 'nest_id',
  15. 'name',
  16. 'description',
  17. 'docker_image',
  18. 'startup',
  19. 'environment',
  20. 'updated_at',
  21. ];
  22. public static function boot()
  23. {
  24. parent::boot(); // TODO: Change the autogenerated stub
  25. static::deleting(function (Egg $egg) {
  26. $egg->products()->detach();
  27. });
  28. }
  29. public static function syncEggs()
  30. {
  31. Nest::syncNests();
  32. Nest::all()->each(function (Nest $nest) {
  33. $eggs = Pterodactyl::getEggs($nest);
  34. foreach ($eggs as $egg) {
  35. $array = [];
  36. $environment = [];
  37. $array['id'] = $egg['attributes']['id'];
  38. $array['nest_id'] = $egg['attributes']['nest'];
  39. $array['name'] = $egg['attributes']['name'];
  40. $array['description'] = $egg['attributes']['description'];
  41. $array['docker_image'] = $egg['attributes']['docker_image'];
  42. $array['startup'] = $egg['attributes']['startup'];
  43. $array['updated_at'] = now();
  44. //get environment variables
  45. foreach ($egg['attributes']['relationships']['variables']['data'] as $variable) {
  46. $environment[$variable['attributes']['env_variable']] = $variable['attributes']['default_value'];
  47. }
  48. $array['environment'] = json_encode([$environment]);
  49. self::query()->updateOrCreate([
  50. 'id' => $array['id']
  51. ], array_diff_key($array, array_flip(["id"]))
  52. );
  53. }
  54. self::removeDeletedEggs($nest, $eggs);
  55. });
  56. }
  57. /**
  58. * @description remove eggs that have been deleted on pterodactyl
  59. * @param Nest $nest
  60. * @param array $eggs
  61. */
  62. private static function removeDeletedEggs(Nest $nest, array $eggs): void
  63. {
  64. $ids = array_map(function ($data) {
  65. return $data['attributes']['id'];
  66. }, $eggs);
  67. $nest->eggs()->each(function (Egg $egg) use ($ids) {
  68. if (!in_array($egg->id, $ids)) $egg->delete();
  69. });
  70. }
  71. /**
  72. * @return array
  73. */
  74. public function getEnvironmentVariables()
  75. {
  76. $array = [];
  77. foreach (json_decode($this->environment) as $variable) {
  78. foreach ($variable as $key => $value) {
  79. $array[$key] = $value;
  80. }
  81. }
  82. return $array;
  83. }
  84. /**
  85. * @return BelongsTo
  86. */
  87. public function nest()
  88. {
  89. return $this->belongsTo(Nest::class);
  90. }
  91. /**
  92. * @return BelongsToMany
  93. */
  94. public function products()
  95. {
  96. return $this->belongsToMany(Product::class);
  97. }
  98. }