Egg.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. class Egg extends Model
  8. {
  9. use HasFactory;
  10. public $incrementing = false;
  11. protected $fillable = [
  12. 'id',
  13. 'nest_id',
  14. 'name',
  15. 'description',
  16. 'docker_image',
  17. 'startup',
  18. 'environment',
  19. ];
  20. /**
  21. * @return BelongsTo
  22. */
  23. public function nest()
  24. {
  25. return $this->belongsTo(Nest::class, 'id', 'nest_id');
  26. }
  27. /**
  28. * @return array
  29. */
  30. public function getEnvironmentVariables()
  31. {
  32. $array = [];
  33. foreach (json_decode($this->environment) as $variable) {
  34. foreach ($variable as $key => $value){
  35. $array[$key] = $value;
  36. }
  37. }
  38. return $array;
  39. }
  40. public static function syncEggs(){
  41. Nest::all()->each(function (Nest $nest) {
  42. $eggs = Pterodactyl::getEggs($nest);
  43. foreach ($eggs as $egg){
  44. $array = [];
  45. $environment = [];
  46. $array['id'] = $egg['attributes']['id'];
  47. $array['nest_id'] = $egg['attributes']['nest'];
  48. $array['name'] = $egg['attributes']['name'];
  49. $array['description'] = $egg['attributes']['description'];
  50. $array['docker_image'] = $egg['attributes']['docker_image'];
  51. $array['startup'] = $egg['attributes']['startup'];
  52. //get environment variables
  53. foreach ($egg['attributes']['relationships']['variables']['data'] as $variable){
  54. $environment[$variable['attributes']['env_variable']] = $variable['attributes']['default_value'];
  55. }
  56. $array['environment'] = json_encode([$environment]);
  57. self::firstOrCreate(['id' => $array['id']] , $array);
  58. }
  59. });
  60. }
  61. }