Server.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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()) throw new Exception($response['errors'][0]['code']);
  63. });
  64. }
  65. /**
  66. * @return bool
  67. */
  68. public function isSuspended()
  69. {
  70. return !is_null($this->suspended);
  71. }
  72. /**
  73. * @return PromiseInterface|Response
  74. */
  75. public function getPterodactylServer()
  76. {
  77. return Pterodactyl::client()->get("/application/servers/{$this->pterodactyl_id}");
  78. }
  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. }