ChargeCreditsCommand.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Classes\Pterodactyl;
  4. use App\Models\Product;
  5. use App\Models\Server;
  6. use App\Models\User;
  7. use Carbon\Carbon;
  8. use Illuminate\Console\Command;
  9. use Illuminate\Support\Collection;
  10. class ChargeCreditsCommand extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'credits:charge';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Charge all users with active servers';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return string
  37. */
  38. public function handle()
  39. {
  40. return Server::whereNull('suspended')->chunk(10, function ($servers) {
  41. /** @var Server $server */
  42. foreach ($servers as $server) {
  43. /** @var Product $product */
  44. $product = $server->product;
  45. /** @var User $user */
  46. $user = $server->user;
  47. #charge credits / suspend server
  48. if ($user->credits >= $product->getHourlyPrice()){
  49. $this->line("<fg=blue>{$user->name}</> Current credits: <fg=green>{$user->credits}</> Credits to be removed: <fg=red>{$product->getHourlyPrice()}</>");
  50. $user->decrement('credits', $product->getHourlyPrice());
  51. } else {
  52. $this->line("server <fg=blue>{$server->name}</> <fg=red>has been suspended! </>");
  53. $server->suspend();
  54. }
  55. }
  56. });
  57. }
  58. }