Server.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace App\Models;
  3. use App\Classes\PterodactylClient;
  4. use App\Settings\PterodactylSettings;
  5. use Exception;
  6. use GuzzleHttp\Promise\PromiseInterface;
  7. use Hidehalo\Nanoid\Client;
  8. use Illuminate\Database\Eloquent\Factories\HasFactory;
  9. use Illuminate\Database\Eloquent\Model;
  10. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  11. use Illuminate\Database\Eloquent\Relations\HasOne;
  12. use Illuminate\Http\Client\Response;
  13. use Spatie\Activitylog\LogOptions;
  14. use Spatie\Activitylog\Traits\LogsActivity;
  15. /**
  16. * Class Server
  17. */
  18. class Server extends Model
  19. {
  20. use HasFactory;
  21. use LogsActivity;
  22. private PterodactylClient $pterodactyl;
  23. public function getActivitylogOptions(): LogOptions
  24. {
  25. return LogOptions::defaults()
  26. ->logOnlyDirty()
  27. ->logOnly(['*'])
  28. ->dontSubmitEmptyLogs();
  29. }
  30. /**
  31. * @var bool
  32. */
  33. public $incrementing = false;
  34. /**
  35. * @var string[]
  36. */
  37. protected static $ignoreChangedAttributes = ['pterodactyl_id', 'identifier', 'updated_at'];
  38. /**
  39. * @var string[]
  40. */
  41. protected static $logAttributes = ['name', 'description'];
  42. /**
  43. * @var string[]
  44. */
  45. protected $fillable = [
  46. 'name',
  47. 'description',
  48. 'suspended',
  49. 'identifier',
  50. 'product_id',
  51. 'pterodactyl_id',
  52. ];
  53. /**
  54. * @var string[]
  55. */
  56. protected $casts = [
  57. 'suspended' => 'datetime',
  58. ];
  59. public function __construct()
  60. {
  61. parent::__construct();
  62. $ptero_settings = new PterodactylSettings();
  63. $this->pterodactyl = new PterodactylClient($ptero_settings);
  64. }
  65. public static function boot()
  66. {
  67. parent::boot();
  68. static::creating(function (Server $server) {
  69. $client = new Client();
  70. $server->{$server->getKeyName()} = $client->generateId($size = 21);
  71. });
  72. static::deleting(function (Server $server) {
  73. $response = $server->pterodactyl->application->delete("/application/servers/{$server->pterodactyl_id}");
  74. if ($response->failed() && !is_null($server->pterodactyl_id)) {
  75. //only return error when it's not a 404 error
  76. if ($response['errors'][0]['status'] != '404') {
  77. throw new Exception($response['errors'][0]['code']);
  78. }
  79. }
  80. });
  81. }
  82. /**
  83. * @return bool
  84. */
  85. public function isSuspended()
  86. {
  87. return !is_null($this->suspended);
  88. }
  89. /**
  90. * @return PromiseInterface|Response
  91. */
  92. public function getPterodactylServer()
  93. {
  94. return $this->pterodactyl->application->get("/application/servers/{$this->pterodactyl_id}");
  95. }
  96. /**
  97. * @throws Exception
  98. */
  99. public function suspend()
  100. {
  101. $response = $this->pterodactyl->suspendServer($this);
  102. if ($response->successful()) {
  103. $this->update([
  104. 'suspended' => now(),
  105. ]);
  106. }
  107. return $this;
  108. }
  109. /**
  110. * @throws Exception
  111. */
  112. public function unSuspend()
  113. {
  114. $response = $this->pterodactyl->unSuspendServer($this);
  115. if ($response->successful()) {
  116. $this->update([
  117. 'suspended' => null,
  118. ]);
  119. }
  120. return $this;
  121. }
  122. /**
  123. * @return HasOne
  124. */
  125. public function product()
  126. {
  127. return $this->hasOne(Product::class, 'id', 'product_id');
  128. }
  129. /**
  130. * @return BelongsTo
  131. */
  132. public function user()
  133. {
  134. return $this->belongsTo(User::class, 'user_id', 'id');
  135. }
  136. }