Egg.php 3.2 KB

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