Egg.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. *
  60. * @param Nest $nest
  61. * @param array $eggs
  62. */
  63. private static function removeDeletedEggs(Nest $nest, array $eggs): void
  64. {
  65. $ids = array_map(function ($data) {
  66. return $data['attributes']['id'];
  67. }, $eggs);
  68. $nest->eggs()->each(function (Egg $egg) use ($ids) {
  69. if (! in_array($egg->id, $ids)) {
  70. $egg->delete();
  71. }
  72. });
  73. }
  74. /**
  75. * @return array
  76. */
  77. public function getEnvironmentVariables()
  78. {
  79. $array = [];
  80. foreach (json_decode($this->environment) as $variable) {
  81. foreach ($variable as $key => $value) {
  82. $array[$key] = $value;
  83. }
  84. }
  85. return $array;
  86. }
  87. /**
  88. * @return BelongsTo
  89. */
  90. public function nest()
  91. {
  92. return $this->belongsTo(Nest::class);
  93. }
  94. /**
  95. * @return BelongsToMany
  96. */
  97. public function products()
  98. {
  99. return $this->belongsToMany(Product::class);
  100. }
  101. }