Server.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. ];
  53. /**
  54. * @var string[]
  55. */
  56. protected $casts = [
  57. 'suspended' => 'datetime',
  58. ];
  59. public static function boot()
  60. {
  61. parent::boot();
  62. static::creating(function (Server $server) {
  63. $client = new Client();
  64. $server->{$server->getKeyName()} = $client->generateId($size = 21);
  65. });
  66. static::deleting(function (Server $server) {
  67. $response = Pterodactyl::client()->delete("/application/servers/{$server->pterodactyl_id}");
  68. if ($response->failed() && ! is_null($server->pterodactyl_id)) {
  69. //only return error when it's not a 404 error
  70. if ($response['errors'][0]['status'] != '404') {
  71. throw new Exception($response['errors'][0]['code']);
  72. }
  73. }
  74. });
  75. }
  76. /**
  77. * @return bool
  78. */
  79. public function isSuspended()
  80. {
  81. return ! is_null($this->suspended);
  82. }
  83. /**
  84. * @return PromiseInterface|Response
  85. */
  86. public function getPterodactylServer()
  87. {
  88. return Pterodactyl::client()->get("/application/servers/{$this->pterodactyl_id}");
  89. }
  90. /**
  91. * @throws Exception
  92. */
  93. public function suspend()
  94. {
  95. $response = Pterodactyl::suspendServer($this);
  96. if ($response->successful()) {
  97. $this->update([
  98. 'suspended' => now(),
  99. ]);
  100. }
  101. return $this;
  102. }
  103. /**
  104. * @throws Exception
  105. */
  106. public function unSuspend()
  107. {
  108. $response = Pterodactyl::unSuspendServer($this);
  109. if ($response->successful()) {
  110. $this->update([
  111. 'suspended' => null,
  112. 'last_billed' => Carbon::now()->toDateTimeString(),
  113. ]);
  114. }
  115. return $this;
  116. }
  117. /**
  118. * @return HasOne
  119. */
  120. public function product()
  121. {
  122. return $this->hasOne(Product::class, 'id', 'product_id');
  123. }
  124. /**
  125. * @return BelongsTo
  126. */
  127. public function user()
  128. {
  129. return $this->belongsTo(User::class, 'user_id', 'id');
  130. }
  131. }