|
- <?php
-
- namespace App\Console\Commands;
-
- use App\Exceptions\AppCommonException;
- use App\Logic\SMS\SMSManager;
- use App\Logic\User\AdminUserManager;
- use App\Models\User;
- use App\Util\DBUtil;
- use Exception;
- use Illuminate\Support\Arr;
-
- class CreateAdminUser extends BaseCommand
- {
-
- const COMMAND = "create:admin-user {--email=} {--name=} {--password=}";
-
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = self::COMMAND;
-
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '管理者ユーザーを作成する';
-
-
- 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
- {
- $db = DBUtil::instance();
-
- $params = $this->getParams();
-
-
- try {
- $db->beginTransaction();
-
- $manager = new AdminUserManager();
- $messages = $manager->initForCreateAdmin()
- ->fill($params)
- ->create();
-
- if (count($messages) !== 0) {
- throw new AppCommonException(Arr::first($messages));
- }
-
- $db->commit();
- } catch (Exception $e) {
- $db->rollBack();
- throw $e;
- }
-
- return self::RESULTCODE_SUCCESS;
- }
-
- private function getParams(): array
- {
-
- $email = $this->option("email");
- if (!$email) {
- throw new Exception("引数不正:email");
- }
- $name = $this->option("name");
- if (!$name) {
- throw new Exception("引数不正:name");
- }
- $password = $this->option("password");
- if (!$password) {
- throw new Exception("引数不正:password");
- }
-
-
- return [
- User::COL_NAME_NAME => $name,
- User::COL_NAME_EMAIL => $email,
- User::COL_NAME_PASSWORD => $password,
- ];
- }
- }
|