領収証発行サービス
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.3KB

  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Events\SMS\ConfirmEvent;
  4. use App\Exceptions\AppCommonException;
  5. use App\Logic\SMS\SMSManager;
  6. use App\Models\SMSSendOrder;
  7. use App\Util\DBUtil;
  8. class PollSMSSendOrder extends BaseCommand
  9. {
  10. const COMMAND = "sms:poll";
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = self::COMMAND;
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'SMS配信状況を取得/反映する';
  23. static public function getCommand()
  24. {
  25. return self::COMMAND;
  26. }
  27. /**
  28. * Create a new command instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct(private SMSManager $manager)
  33. {
  34. parent::__construct();
  35. }
  36. /**
  37. * Execute the console command.
  38. *
  39. * @return int
  40. */
  41. public function service(): int
  42. {
  43. $targets = $this->getTargets();
  44. $this->outputInfo(sprintf("対象:%d件", $targets->count()));
  45. if ($targets->isEmpty()) {
  46. return self::RESULTCODE_SUCCESS;
  47. } else {
  48. }
  49. foreach ($targets as $order) {
  50. $this->handleOrder($order);
  51. }
  52. return self::RESULTCODE_SUCCESS;
  53. }
  54. public function getTargets()
  55. {
  56. return SMSSendOrder::whereDone(false)
  57. ->get();
  58. }
  59. public function handleOrder(SMSSendOrder $order)
  60. {
  61. $db = DBUtil::instance();
  62. try {
  63. $db->beginTransaction();
  64. $ret = $this->manager->setOrder($order)
  65. ->poll();
  66. if ($ret) {
  67. $order->save();
  68. $this->outputInfo(sprintf(
  69. "ID:%s 電話番号:%s %s",
  70. $order->id,
  71. $order->phone_number,
  72. $order->done ? "完了" : "未"
  73. ));
  74. // イベント発行
  75. if ($order->done) {
  76. ConfirmEvent::dispatch($order);
  77. }
  78. } else {
  79. $this->outputError(printf("失敗対象:%s", $order->toJson()));
  80. throw new AppCommonException("POLL 失敗");
  81. }
  82. $db->commit();
  83. } catch (Exception $e) {
  84. $db->rollBack();
  85. throw $e;
  86. }
  87. }
  88. }