getData(); if ($validator->fails()) { $this->errorMessageAndStop($validator->errors()->all()); } $data = $validator->valid(); try { $role = $roleRepository->getRoleBySlug(SystemRole::Admin->value); if (is_null($role)) { $this->errorMessageAndStop('Administrator role not found.'); } $user = DB::transaction(function () use($data, $userCommandHandler, $role) { $data['name'] = 'Administrator'; $user = $userCommandHandler->handleStore($data, $data['password']); $userCommandHandler->handleConfirmationByEmail($user); $roles = new ManyRoleDto([$role->id]); $userCommandHandler->handleSyncRoles($user, $roles); return $user; }); } catch (\Throwable $e) { $this->errorMessageAndStop($e->getMessage()); } $this->info('The command was successful!'); } private function getData(): Validator { return ValidatorFacade::make([ 'email' => $this->argument('email'), 'password' => $this->argument('password'), ], [ 'email' => ['required', 'email', 'unique:users,email'], 'password' => ['required', Password::default()], ]); } private function stop(): never { exit; } private function errorMessageAndStop(string | array $error): never { $this->info('User not created. See error messages below:'); if (is_array($error)) { foreach ($error as $err) { $this->error($err); } } else { $this->error($error); } $this->stop(); } }