領収証発行サービス
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

106 lines
2.2KB

  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Exceptions\AppCommonException;
  4. use App\Logic\SMS\SMSManager;
  5. use App\Logic\User\AdminUserManager;
  6. use App\Models\User;
  7. use App\Util\DBUtil;
  8. use Exception;
  9. use Illuminate\Support\Arr;
  10. class CreateAdminUser extends BaseCommand
  11. {
  12. const COMMAND = "create:admin-user {--email=} {--name=} {--password=}";
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = self::COMMAND;
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = '管理者ユーザーを作成する';
  25. static public function getCommand()
  26. {
  27. return self::COMMAND;
  28. }
  29. /**
  30. * Create a new command instance.
  31. *
  32. * @return void
  33. */
  34. public function __construct(private SMSManager $manager)
  35. {
  36. parent::__construct();
  37. }
  38. /**
  39. * Execute the console command.
  40. *
  41. * @return int
  42. */
  43. public function service(): int
  44. {
  45. $db = DBUtil::instance();
  46. $params = $this->getParams();
  47. try {
  48. $db->beginTransaction();
  49. $manager = new AdminUserManager();
  50. $messages = $manager->initForCreateAdmin()
  51. ->fill($params)
  52. ->create();
  53. if (count($messages) !== 0) {
  54. throw new AppCommonException(Arr::first($messages));
  55. }
  56. $db->commit();
  57. } catch (Exception $e) {
  58. $db->rollBack();
  59. throw $e;
  60. }
  61. return self::RESULTCODE_SUCCESS;
  62. }
  63. private function getParams(): array
  64. {
  65. $email = $this->option("email");
  66. if (!$email) {
  67. throw new Exception("引数不正:email");
  68. }
  69. $name = $this->option("name");
  70. if (!$name) {
  71. throw new Exception("引数不正:name");
  72. }
  73. $password = $this->option("password");
  74. if (!$password) {
  75. throw new Exception("引数不正:password");
  76. }
  77. return [
  78. User::COL_NAME_NAME => $name,
  79. User::COL_NAME_EMAIL => $email,
  80. User::COL_NAME_PASSWORD => $password,
  81. ];
  82. }
  83. }