Server.php 3.4 KB

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