Server.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. $ptero_settings = new PterodactylSettings();
  62. $this->pterodactyl = new PterodactylClient($ptero_settings);
  63. }
  64. public static function boot()
  65. {
  66. parent::boot();
  67. static::creating(function (Server $server) {
  68. $client = new Client();
  69. $server->{$server->getKeyName()} = $client->generateId($size = 21);
  70. });
  71. static::deleting(function (Server $server) {
  72. $response = $server->pterodactyl->application->delete("/application/servers/{$server->pterodactyl_id}");
  73. if ($response->failed() && !is_null($server->pterodactyl_id)) {
  74. //only return error when it's not a 404 error
  75. if ($response['errors'][0]['status'] != '404') {
  76. throw new Exception($response['errors'][0]['code']);
  77. }
  78. }
  79. });
  80. }
  81. /**
  82. * @return bool
  83. */
  84. public function isSuspended()
  85. {
  86. return !is_null($this->suspended);
  87. }
  88. /**
  89. * @return PromiseInterface|Response
  90. */
  91. public function getPterodactylServer()
  92. {
  93. return $this->pterodactyl->application->get("/application/servers/{$this->pterodactyl_id}");
  94. }
  95. /**
  96. * @throws Exception
  97. */
  98. public function suspend()
  99. {
  100. $response = $this->pterodactyl->suspendServer($this);
  101. if ($response->successful()) {
  102. $this->update([
  103. 'suspended' => now(),
  104. ]);
  105. }
  106. return $this;
  107. }
  108. /**
  109. * @throws Exception
  110. */
  111. public function unSuspend()
  112. {
  113. $response = $this->pterodactyl->unSuspendServer($this);
  114. if ($response->successful()) {
  115. $this->update([
  116. 'suspended' => null,
  117. ]);
  118. }
  119. return $this;
  120. }
  121. /**
  122. * @return HasOne
  123. */
  124. public function product()
  125. {
  126. return $this->hasOne(Product::class, 'id', 'product_id');
  127. }
  128. /**
  129. * @return BelongsTo
  130. */
  131. public function user()
  132. {
  133. return $this->belongsTo(User::class, 'user_id', 'id');
  134. }
  135. }