-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from andriusvo/feature/upgrade-platform
Platform upgrade Sylius & PHP & Symfony.
- Loading branch information
Showing
16 changed files
with
142 additions
and
346 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,49 +7,39 @@ | |
use Platform\Bundle\AdminBundle\Model\AdminUserInterface; | ||
use Sylius\Component\Resource\Factory\FactoryInterface; | ||
use Sylius\Component\User\Repository\UserRepositoryInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\QuestionHelper; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Question\Question; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Validator\Constraints\Email; | ||
use Symfony\Component\Validator\Constraints\NotBlank; | ||
use Symfony\Component\Validator\ConstraintViolationListInterface; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
class SetupCommand extends AbstractInstallCommand | ||
{ | ||
const DEFAULT_USER_EMAIL = '[email protected]'; | ||
const DEFAULT_USER_PASSWORD = 'admin-platform'; | ||
|
||
/** | ||
* @var LocaleSetup | ||
*/ | ||
private $localeSetup; | ||
|
||
/** | ||
* @var EntityManagerInterface | ||
*/ | ||
private $userManager; | ||
|
||
/** | ||
* @var FactoryInterface | ||
*/ | ||
private $userFactory; | ||
|
||
/** | ||
* @var UserRepositoryInterface | ||
*/ | ||
private $userRepository; | ||
|
||
/** | ||
* @var ValidatorInterface | ||
*/ | ||
private $validator; | ||
|
||
public function __construct(LocaleSetup $localeSetup, EntityManagerInterface $userManager, FactoryInterface $userFactory, UserRepositoryInterface $userRepository, ValidatorInterface $validator) | ||
{ | ||
protected const DEFAULT_USER_EMAIL = '[email protected]'; | ||
protected const DEFAULT_USER_PASSWORD = 'admin-platform'; | ||
|
||
private LocaleSetup $localeSetup; | ||
|
||
private EntityManagerInterface $userManager; | ||
|
||
private FactoryInterface $userFactory; | ||
|
||
private UserRepositoryInterface $userRepository; | ||
|
||
private ValidatorInterface $validator; | ||
|
||
public function __construct( | ||
LocaleSetup $localeSetup, | ||
EntityManagerInterface $userManager, | ||
FactoryInterface $userFactory, | ||
UserRepositoryInterface $userRepository, | ||
ValidatorInterface $validator | ||
) { | ||
parent::__construct(); | ||
|
||
$this->localeSetup = $localeSetup; | ||
|
@@ -59,47 +49,36 @@ public function __construct(LocaleSetup $localeSetup, EntityManagerInterface $us | |
$this->validator = $validator; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
protected function configure(): void | ||
{ | ||
$this | ||
->setName('admin-platform:install:setup') | ||
->setDescription('Admin platform configuration setup.') | ||
->setHelp(<<<EOT | ||
->setHelp( | ||
<<<EOT | ||
The <info>%command.name%</info> command allows user to configure basic Admin platform data. | ||
EOT | ||
) | ||
; | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$locale = $this->localeSetup->setup($input, $output); | ||
|
||
$this->setupAdministratorUser($input, $output, $locale->getCode()); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
/** | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
* @param $localeCode | ||
* | ||
* @return int | ||
*/ | ||
private function setupAdministratorUser(InputInterface $input, OutputInterface $output, $localeCode) | ||
private function setupAdministratorUser(InputInterface $input, OutputInterface $output, ?string $localeCode): void | ||
{ | ||
$outputStyle = new SymfonyStyle($input, $output); | ||
$outputStyle->writeln('Create your administrator account.'); | ||
|
||
try { | ||
$user = $this->configureNewUser($this->userFactory->createNew(), $input, $output); | ||
} catch (\InvalidArgumentException $exception) { | ||
return 0; | ||
return; | ||
} | ||
|
||
$user->setEnabled(true); | ||
|
@@ -112,15 +91,11 @@ private function setupAdministratorUser(InputInterface $input, OutputInterface $ | |
$outputStyle->newLine(); | ||
} | ||
|
||
/** | ||
* @param AdminUserInterface $user | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
* | ||
* @return AdminUserInterface | ||
*/ | ||
private function configureNewUser(AdminUserInterface $user, InputInterface $input, OutputInterface $output) | ||
{ | ||
private function configureNewUser( | ||
AdminUserInterface $user, | ||
InputInterface $input, | ||
OutputInterface $output | ||
): AdminUserInterface { | ||
if ($input->getOption('no-interaction')) { | ||
Assert::null($this->userRepository->findOneByEmail(self::DEFAULT_USER_EMAIL)); | ||
|
||
|
@@ -148,37 +123,27 @@ private function configureNewUser(AdminUserInterface $user, InputInterface $inpu | |
return $user; | ||
} | ||
|
||
/** | ||
* @param OutputInterface $output | ||
* | ||
* @return Question | ||
*/ | ||
private function createEmailQuestion(OutputInterface $output) | ||
private function createEmailQuestion(OutputInterface $output): Question | ||
{ | ||
return (new Question('E-mail:')) | ||
->setValidator(function ($value) use ($output) { | ||
/** @var ConstraintViolationListInterface $errors */ | ||
$errors = $this->validator->validate((string) $value, [new Email(), new NotBlank()]); | ||
foreach ($errors as $error) { | ||
throw new \DomainException($error->getMessage()); | ||
->setValidator( | ||
function ($value) use ($output) { | ||
$errors = $this->validator->validate((string)$value, [new Email(), new NotBlank()]); | ||
foreach ($errors as $error) { | ||
throw new \DomainException($error->getMessage()); | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
return $value; | ||
}) | ||
) | ||
->setMaxAttempts(3); | ||
} | ||
|
||
/** | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
* | ||
* @return mixed | ||
*/ | ||
private function getAdministratorPassword(InputInterface $input, OutputInterface $output) | ||
private function getAdministratorPassword(InputInterface $input, OutputInterface $output): string | ||
{ | ||
/** @var QuestionHelper $questionHelper */ | ||
$questionHelper = $this->getHelper('question'); | ||
$validator = $this->getPasswordQuestionValidator($output); | ||
$validator = $this->getPasswordQuestionValidator(); | ||
|
||
do { | ||
$passwordQuestion = $this->createPasswordQuestion('Choose password:', $validator); | ||
|
@@ -194,15 +159,9 @@ private function getAdministratorPassword(InputInterface $input, OutputInterface | |
return $password; | ||
} | ||
|
||
/** | ||
* @param OutputInterface $output | ||
* | ||
* @return \Closure | ||
*/ | ||
private function getPasswordQuestionValidator(OutputInterface $output) | ||
private function getPasswordQuestionValidator() | ||
{ | ||
return function ($value) use ($output) { | ||
/** @var ConstraintViolationListInterface $errors */ | ||
return function ($value) { | ||
$errors = $this->validator->validate($value, [new NotBlank()]); | ||
foreach ($errors as $error) { | ||
throw new \DomainException($error->getMessage()); | ||
|
@@ -212,13 +171,7 @@ private function getPasswordQuestionValidator(OutputInterface $output) | |
}; | ||
} | ||
|
||
/** | ||
* @param string $message | ||
* @param \Closure $validator | ||
* | ||
* @return Question | ||
*/ | ||
private function createPasswordQuestion($message, \Closure $validator) | ||
private function createPasswordQuestion(string $message, \Closure $validator): Question | ||
{ | ||
return (new Question($message)) | ||
->setValidator($validator) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.