diff --git a/app/Console/Commands/CreateFirstUser.php b/app/Console/Commands/CreateFirstUser.php new file mode 100644 index 0000000000..c5fdf4a7bb --- /dev/null +++ b/app/Console/Commands/CreateFirstUser.php @@ -0,0 +1,89 @@ +error('This command only works in the testing environment.'); + return 1; + } + $this->stupidLaravel(); + $count = $this->repository->count(); + if ($count > 0) { + $this->error('Already have more than zero users in DB.'); + return 1; + } + $data = [ + 'blocked' => false, + 'blocked_code' => null, + 'email' => $this->argument('email'), + 'role' => 'owner', + ]; + $password = Str::random(24); + $user = $this->repository->store($data); + $user->password = Hash::make($password); + $user->save(); + $user->setRememberToken(Str::random(60)); + + $this->info(sprintf('Created new admin user (ID #%d) with email address "%s" and password "%s".', $user->id, $user->email, $password)); + $this->error('Change this password.'); + return 0; + } + + /** + * Laravel will execute ALL __construct() methods for ALL commands whenever a SINGLE command is + * executed. This leads to noticeable slow-downs and class calls. To prevent this, this method should + * be called from the handle method instead of using the constructor to initialize the command. + * + * @codeCoverageIgnore + */ + private function stupidLaravel(): void + { + $this->repository = app(UserRepositoryInterface::class); + + } +} diff --git a/app/Repositories/User/UserRepository.php b/app/Repositories/User/UserRepository.php index f84ec0e2ed..c2f681e602 100644 --- a/app/Repositories/User/UserRepository.php +++ b/app/Repositories/User/UserRepository.php @@ -36,16 +36,6 @@ use Str; */ class UserRepository implements UserRepositoryInterface { - /** - * Constructor. - */ - public function __construct() - { - if ('testing' === config('app.env')) { - Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); - } - } - /** * @return Collection */