Server.php 3.2 KB

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