Server.php 3.0 KB

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