Skip to content

Commit

Permalink
Merge pull request #13 from andriusvo/feature/upgrade-platform
Browse files Browse the repository at this point in the history
Platform upgrade Sylius & PHP & Symfony.
  • Loading branch information
GrandLTU authored Apr 8, 2022
2 parents 9d397ca + 690d42c commit c986ce5
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 346 deletions.
19 changes: 8 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@
"description": "Admin platform for Symfony projects.",
"license": "MIT",
"require": {
"php": "^7.2",

"sylius/grid-bundle": "^1.3.0",
"sylius/locale-bundle": "^1.3.0",
"sylius/mailer-bundle": "^1.3.0",
"sylius/resource-bundle": "^1.3.0",
"sylius/ui-bundle": "^1.3.0",
"sylius/user-bundle": "^1.3.0",
"symfony/process": "^3.4|^4.1"
},
"require-dev": {
"php": "^8.0",
"sylius/grid-bundle": "^1.10",
"sylius/locale-bundle": "^1.11",
"sylius/mailer-bundle": "^1.6",
"sylius/resource-bundle": "^1.9",
"sylius/ui-bundle": "^1.11",
"sylius/user-bundle": "^1.11",
"symfony/process": "^6.0"
},
"config": {
"bin-dir": "bin"
Expand Down
15 changes: 7 additions & 8 deletions src/Command/AbstractInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@

abstract class AbstractInstallCommand extends Command
{
/**
* @var CommandExecutor
*/
protected $commandExecutor;
protected CommandExecutor $commandExecutor;

/**
* {@inheritdoc}
*/
protected function initialize(InputInterface $input, OutputInterface $output)
protected function initialize(InputInterface $input, OutputInterface $output): void
{
$application = $this->getApplication();

if ($application === null) {
return;
}

$application->setCatchExceptions(false);

$this->commandExecutor = new CommandExecutor(
Expand Down
47 changes: 9 additions & 38 deletions src/Command/CommandExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,20 @@

class CommandExecutor
{
/**
* @var InputInterface
*/
protected $input;

/**
* @var OutputInterface
*/
protected $output;

/**
* @var Application
*/
protected $application;

/**
* @param InputInterface $input
* @param OutputInterface $output
* @param Application $application
*/
protected InputInterface $input;

protected OutputInterface $output;

protected Application $application;

public function __construct(InputInterface $input, OutputInterface $output, Application $application)
{
$this->input = $input;
$this->output = $output;
$this->application = $application;
}

/**
* @param $command
* @param array $parameters
* @param OutputInterface $output
*
* @return $this
*
* @throws \Exception
*/
public function runCommand($command, $parameters = [], OutputInterface $output = null)
public function runCommand($command, $parameters = [], OutputInterface $output = null): self
{
$parameters = array_merge(
['command' => $command],
Expand All @@ -67,20 +44,14 @@ public function runCommand($command, $parameters = [], OutputInterface $output =

$errorMessage = sprintf('The command terminated with an error code: %u.', $exitCode);
$this->output->writeln("<error>$errorMessage</error>");
$exception = new \Exception($errorMessage, $exitCode);

throw $exception;
throw new \Exception($errorMessage, $exitCode);
}

return $this;
}

/**
* Get default parameters.
*
* @return array
*/
protected function getDefaultParameters()
protected function getDefaultParameters(): array
{
$defaultParameters = ['--no-debug' => true];

Expand Down
147 changes: 50 additions & 97 deletions src/Command/SetupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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));

Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand All @@ -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)
Expand Down
13 changes: 2 additions & 11 deletions src/Context/AdminBasedLocaleContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,13 @@

class AdminBasedLocaleContext implements LocaleContextInterface
{
/**
* @var TokenStorageInterface
*/
private $tokenStorage;

/**
* @param TokenStorageInterface $tokenStorage
*/
private TokenStorageInterface $tokenStorage;

public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}

/**
* {@inheritdoc}
*/
public function getLocaleCode(): string
{
$token = $this->tokenStorage->getToken();
Expand Down
Loading

0 comments on commit c986ce5

Please sign in to comment.