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