Server.php 3.7 KB

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