|
- <?php
-
- namespace App\Console\Commands;
-
- use App\Events\SMS\ConfirmEvent;
- use App\Exceptions\AppCommonException;
- use App\Logic\SMS\SMSManager;
- use App\Models\SMSSendOrder;
- use App\Util\DBUtil;
-
- class PollSMSSendOrder extends BaseCommand
- {
-
- const COMMAND = "sms:poll";
-
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = self::COMMAND;
-
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'SMS配信状況を取得/反映する';
-
- static public function getCommand()
- {
- return self::COMMAND;
- }
-
-
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct(private SMSManager $manager)
- {
- parent::__construct();
- }
-
- /**
- * Execute the console command.
- *
- * @return int
- */
- public function service(): int
- {
- $targets = $this->getTargets();
-
- $this->outputInfo(sprintf("対象:%d件", $targets->count()));
- if ($targets->isEmpty()) {
- return self::RESULTCODE_SUCCESS;
- } else {
- }
-
- foreach ($targets as $order) {
- $this->handleOrder($order);
- }
-
- return self::RESULTCODE_SUCCESS;
- }
-
- public function getTargets()
- {
- return SMSSendOrder::whereDone(false)
- ->get();
- }
-
- public function handleOrder(SMSSendOrder $order)
- {
-
- $db = DBUtil::instance();
-
- try {
- $db->beginTransaction();
-
- $ret = $this->manager->setOrder($order)
- ->poll();
-
- if ($ret) {
- $order->save();
-
- $this->outputInfo(sprintf(
- "ID:%s 電話番号:%s %s",
- $order->id,
- $order->phone_number,
- $order->done ? "完了" : "未"
- ));
-
- // イベント発行
- if ($order->done) {
- ConfirmEvent::dispatch($order);
- }
- } else {
- $this->outputError(printf("失敗対象:%s", $order->toJson()));
- throw new AppCommonException("POLL 失敗");
- }
-
- $db->commit();
- } catch (Exception $e) {
- $db->rollBack();
- throw $e;
- }
- }
- }
|