Skip to content

Commit

Permalink
Merge pull request akeneo#74 from akeneo/default-responses-parameters
Browse files Browse the repository at this point in the history
Add possibility to define defaults responses
  • Loading branch information
LaurentPetard committed Oct 2, 2017
2 parents 8327a14 + 7b24e0a commit c4f349f
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 29 deletions.
1 change: 1 addition & 0 deletions src/Infrastructure/Common/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public static function getContainer(): Container

$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/config'));
$loader->load('services.yml');
$loader->load('parameters.yml');

self::loadTranslatorConfiguration($container);
self::loadLoggerConfiguration($container);
Expand Down
16 changes: 16 additions & 0 deletions src/Infrastructure/Common/config/parameters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
parameters:
default_responses:
installation_path_source_pim: ''
installation_path_destination_pim: ''

ssh_hostname_source_pim: ''
ssh_port_source_pim: '22'
ssh_user_source_pim: ''
ssh_key_path_source_pim: ''

api_base_uri_source_pim: ''
api_base_uri_destination_pim: ''
api_client_id: ''
api_secret: ''
api_user_name: ''
api_user_pwd: ''
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function onDistantConfiguration(Event $event)

$host = $this->printerAndAsker->askSimpleQuestion(
$this->translator->trans($transPrefix.'hostname_question'),
'',
$stateMachine->getDefaultResponse('ssh_hostname_source_pim'),
function ($answer) use ($transPrefix) {
if (preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $answer)
&& preg_match('/^.{1,253}$/', $answer)
Expand All @@ -145,7 +145,7 @@ function ($answer) use ($transPrefix) {

$port = (int) $this->printerAndAsker->askSimpleQuestion(
$this->translator->trans($transPrefix.'ssh_port_question'),
'22',
$stateMachine->getDefaultResponse('ssh_port_source_pim'),
function ($answer) use ($transPrefix) {
if (!is_numeric($answer)) {
throw new \RuntimeException($this->translator->trans($transPrefix.'ssh_port_error'));
Expand All @@ -154,13 +154,16 @@ function ($answer) use ($transPrefix) {
return $answer;
}
);
$user = $this->printerAndAsker->askSimpleQuestion($this->translator->trans($transPrefix.'ssh_user_question'));
$user = $this->printerAndAsker->askSimpleQuestion(
$this->translator->trans($transPrefix.'ssh_user_question'),
$stateMachine->getDefaultResponse('ssh_user_source_pim')
);

$sshPath = $this
->printerAndAsker
->askSimpleQuestion(
$this->translator->trans($transPrefix.'ssh_key_path_question'),
'',
$stateMachine->getDefaultResponse('ssh_key_path_source_pim'),
function ($answer) use ($transPrefix) {
$fs = new Filesystem();

Expand All @@ -178,7 +181,7 @@ function ($answer) use ($transPrefix) {
->printerAndAsker
->askSimpleQuestion(
$this->translator->trans($transPrefix.'project_path_question'),
'',
$stateMachine->getDefaultResponse('installation_path_source_pim'),
function ($answer) use ($transPrefix) {
$fs = new Filesystem();

Expand Down Expand Up @@ -228,7 +231,7 @@ public function onLocalConfiguration(Event $event)
->printerAndAsker
->askSimpleQuestion(
$this->translator->trans($transPrefix.'project_path_question'),
'',
$stateMachine->getDefaultResponse('installation_path_source_pim'),
function ($answer) use ($transPrefix) {
$fs = new Filesystem();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public function onSourcePimApiConfiguration(Event $event)
$stateMachine = $event->getSubject();

$sourceApiParameters = new PimApiParameters(
$this->askForBaseUri(),
$this->askForClientId(),
$this->askForSecret(),
$this->askForUserName(),
$this->askForUserPwd()
$this->askForBaseUri($stateMachine->getDefaultResponse('api_base_uri_source_pim')),
$this->askForClientId($stateMachine->getDefaultResponse('api_client_id')),
$this->askForSecret($stateMachine->getDefaultResponse('api_secret')),
$this->askForUserName($stateMachine->getDefaultResponse('api_user_name')),
$this->askForUserPwd($stateMachine->getDefaultResponse('api_user_pwd'))
);

try {
Expand All @@ -62,7 +62,7 @@ public function onSourcePimApiConfiguration(Event $event)
$stateMachine->setSourcePimApiParameters($sourceApiParameters);
}

private function askForBaseUri(): string
private function askForBaseUri(string $defaultResponse): string
{
$question = $this->translator->trans(
'from_source_pim_configured_to_source_pim_api_configured.on_source_pim_api_configuration.base_uri.question'
Expand All @@ -80,42 +80,42 @@ private function askForBaseUri(): string
}
};

return $this->printerAndAsker->askSimpleQuestion($question, '', $validator);
return $this->printerAndAsker->askSimpleQuestion($question, $defaultResponse, $validator);
}

private function askForClientId(): string
private function askForClientId(string $defaultResponse): string
{
$question = $this->translator->trans(
'from_source_pim_configured_to_source_pim_api_configured.on_source_pim_api_configuration.client_id_question'
);

return $this->printerAndAsker->askSimpleQuestion($question);
return $this->printerAndAsker->askSimpleQuestion($question, $defaultResponse);
}

private function askForSecret(): string
private function askForSecret(string $defaultResponse): string
{
$question = $this->translator->trans(
'from_source_pim_configured_to_source_pim_api_configured.on_source_pim_api_configuration.secret_question'
);

return $this->printerAndAsker->askSimpleQuestion($question);
return $this->printerAndAsker->askSimpleQuestion($question, $defaultResponse);
}

private function askForUserName(): string
private function askForUserName(string $defaultResponse): string
{
$question = $this->translator->trans(
'from_source_pim_configured_to_source_pim_api_configured.on_source_pim_api_configuration.user_name_question'
);

return $this->printerAndAsker->askSimpleQuestion($question);
return $this->printerAndAsker->askSimpleQuestion($question, $defaultResponse);
}

private function askForUserPwd(): string
private function askForUserPwd(string $defaultResponse): string
{
$question = $this->translator->trans(
'from_source_pim_configured_to_source_pim_api_configured.on_source_pim_api_configuration.user_pwd_question'
);

return $this->printerAndAsker->askSimpleQuestion($question);
return $this->printerAndAsker->askSimpleQuestion($question, $defaultResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function onAskDestinationPimLocation(Event $event)

$destinationPath = $this->printerAndAsker->askSimpleQuestion(
$this->translator->trans($transPrefix.'local_pim_path_question'),
'',
$stateMachine->getDefaultResponse('installation_path_destination_pim'),
function ($answer) use ($transPrefix) {
$fs = new Filesystem();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function onDestinationPimApiConfiguration(Event $event)
$this
->translator
->trans('from_destination_pim_downloaded_to_destination_pim_installed.on_destination_pim_api_configuration.base_uri.question'),
'',
$stateMachine->getDefaultResponse('api_base_uri_destination_pim'),
function ($answer) {
// This URI validation regex is intentionally imperfect.
// It's goal is only to avoid common mistakes like forgetting "http", or adding parameters from a copy/paste.
Expand Down
13 changes: 13 additions & 0 deletions src/Infrastructure/TransporteoStateMachine.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ class TransporteoStateMachine
/** @var LoggerInterface */
protected $logger;

/** @var array */
protected $defaultResponses = [];

public function __construct(StateMachine $stateMachine, LoggerInterface $logger)
{
$this->stateMachineMarker = $stateMachine;
Expand Down Expand Up @@ -249,4 +252,14 @@ public function setDestinationPimApiParameters(PimApiParameters $destinationPimA
{
$this->destinationPimApiParameters = $destinationPimApiParameters;
}

public function setDefaultResponses(array $defaultResponses): void
{
$this->defaultResponses = $defaultResponses;
}

public function getDefaultResponse(string $question): string
{
return $this->defaultResponses[$question] ?? '';
}
}
1 change: 1 addition & 0 deletions src/Infrastructure/UserInterface/Cli/Transporteo.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): void
{
$stateMachine = new TransporteoStateMachine($this->container->get('state_machine.transporteo'), $this->container->get(LoggerInterface::class));
$stateMachine->setDefaultResponses($this->container->getParameter('default_responses'));

$cliQuestionAsker = new ConsolePrinterAndAsker($input, $output, $this->getHelper('question'));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,10 @@ public function it_configures_a_source_pim_from_a_server(

$event->getSubject()->willReturn($stateMachine);
$stateMachine->getProjectName()->willReturn('a-super-project');
$stateMachine->getDefaultResponse(Argument::any())->willReturn('');

$printerAndAsker->askSimpleQuestion($hostNameQuestion, Argument::any(), Argument::any())->willReturn('my-super-pim.akeneo.com');
$printerAndAsker->askSimpleQuestion($portQuestion, '22', Argument::any())->willReturn('22');
$printerAndAsker->askSimpleQuestion($portQuestion, '', Argument::any())->willReturn('22');
$printerAndAsker->askSimpleQuestion($sshUserQuestion, Argument::any(), Argument::any())->willReturn('akeneo');
$sshKeyPath = ResourcesFileLocator::getSshKeyPath();

Expand Down Expand Up @@ -167,6 +168,7 @@ public function it_configures_a_source_pim_from_local(
) {
$event->getSubject()->willReturn($stateMachine);
$stateMachine->getProjectName()->willReturn('a-super-project');
$stateMachine->getDefaultResponse(Argument::any())->willReturn('');

$projectPathQuestion = 'What is the absolute path of the source PIM on your computer? ';

Expand Down Expand Up @@ -202,6 +204,7 @@ public function it_throws_business_exception_from_technical(

$event->getSubject()->willReturn($stateMachine);
$stateMachine->getProjectName()->willReturn('a-super-project');
$stateMachine->getDefaultResponse(Argument::any())->willReturn('');

$hostNameQuestion = 'What is the hostname of the source PIM server? ';
$portQuestion = 'What is the SSH port of the source PIM server? ';
Expand All @@ -223,7 +226,7 @@ public function it_throws_business_exception_from_technical(
}

$printerAndAsker->askSimpleQuestion($hostNameQuestion, Argument::any(), Argument::any())->willReturn('my-super-pim.akeneo.com');
$printerAndAsker->askSimpleQuestion($portQuestion, '22', Argument::any())->willReturn('22');
$printerAndAsker->askSimpleQuestion($portQuestion, '', Argument::any())->willReturn('22');
$printerAndAsker->askSimpleQuestion($sshUserQuestion, Argument::any(), Argument::any())->willReturn('akeneo');

$sshKeyPath = ResourcesFileLocator::getSshKeyPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function it_configures_a_source_pim_api(
)
{
$event->getSubject()->willReturn($stateMachine);
$stateMachine->getDefaultResponse(Argument::any())->willReturn('');

$baseUri = 'http:https://localhost';
$question = 'What is the base URI to request the API of the source PIM?';
Expand All @@ -60,28 +61,28 @@ public function it_configures_a_source_pim_api(
$translator
->trans('from_source_pim_configured_to_source_pim_api_configured.on_source_pim_api_configuration.client_id_question')
->willReturn($question);
$printerAndAsker->askSimpleQuestion($question)->willReturn($clientId);
$printerAndAsker->askSimpleQuestion($question, '')->willReturn($clientId);

$secret = 'secret';
$question = 'What is the secret associated to this client?';
$translator
->trans('from_source_pim_configured_to_source_pim_api_configured.on_source_pim_api_configuration.secret_question')
->willReturn($question);
$printerAndAsker->askSimpleQuestion($question)->willReturn($secret);
$printerAndAsker->askSimpleQuestion($question, '')->willReturn($secret);

$userName = 'userName';
$question = 'What is the username to use to authenticate to the API of the source PIM?';
$translator
->trans('from_source_pim_configured_to_source_pim_api_configured.on_source_pim_api_configuration.user_name_question')
->willReturn($question);
$printerAndAsker->askSimpleQuestion($question)->willReturn($userName);
$printerAndAsker->askSimpleQuestion($question, '')->willReturn($userName);

$userPwd = 'userPwd';
$question = 'What is the password associated to this username?';
$translator
->trans('from_source_pim_configured_to_source_pim_api_configured.on_source_pim_api_configuration.user_pwd_question')
->willReturn($question);
$printerAndAsker->askSimpleQuestion($question)->willReturn($userPwd);
$printerAndAsker->askSimpleQuestion($question, '')->willReturn($userPwd);

$sourceApiParameters = new PimApiParameters(
$baseUri,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function it_asks_the_pim_location(
$translator->trans($transPrefix.'local_pim_path_question')->willReturn($localPimPathQuestion);

$event->getSubject()->willReturn($stateMachine);
$stateMachine->getDefaultResponse(Argument::any())->willReturn('');

$pimPath = '/an-absolute-pim-path';
$printerAndAsker->askSimpleQuestion($localPimPathQuestion, '', Argument::any())->willReturn($pimPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public function it_configures_the_destination_pim_api(
)
{
$event->getSubject()->willReturn($stateMachine);
$stateMachine->getDefaultResponse(Argument::any())->willReturn('');

$question = 'What is the base URI to request the API of the destination PIM?';
$baseUri = 'http:https://localhost';
Expand Down

0 comments on commit c4f349f

Please sign in to comment.