Server.php 3.0 KB

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