diff --git a/src/Infrastructure/Common/ContainerBuilder.php b/src/Infrastructure/Common/ContainerBuilder.php index b046059..df3ec4b 100644 --- a/src/Infrastructure/Common/ContainerBuilder.php +++ b/src/Infrastructure/Common/ContainerBuilder.php @@ -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); diff --git a/src/Infrastructure/Common/config/parameters.yml b/src/Infrastructure/Common/config/parameters.yml new file mode 100644 index 0000000..73e8fa4 --- /dev/null +++ b/src/Infrastructure/Common/config/parameters.yml @@ -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: '' diff --git a/src/Infrastructure/MigrationStep/S010FromReadyToSourcePimConfigured.php b/src/Infrastructure/MigrationStep/S010FromReadyToSourcePimConfigured.php index ab5bc0f..833ceb9 100644 --- a/src/Infrastructure/MigrationStep/S010FromReadyToSourcePimConfigured.php +++ b/src/Infrastructure/MigrationStep/S010FromReadyToSourcePimConfigured.php @@ -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) @@ -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')); @@ -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(); @@ -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(); @@ -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(); diff --git a/src/Infrastructure/MigrationStep/S015FromSourcePimConfiguredToSourcePimApiConfigured.php b/src/Infrastructure/MigrationStep/S015FromSourcePimConfiguredToSourcePimApiConfigured.php index ee8659f..c244001 100644 --- a/src/Infrastructure/MigrationStep/S015FromSourcePimConfiguredToSourcePimApiConfigured.php +++ b/src/Infrastructure/MigrationStep/S015FromSourcePimConfiguredToSourcePimApiConfigured.php @@ -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 { @@ -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' @@ -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); } } diff --git a/src/Infrastructure/MigrationStep/S040FromSourcePimDetectedToDestinationPimDownloaded.php b/src/Infrastructure/MigrationStep/S040FromSourcePimDetectedToDestinationPimDownloaded.php index 90423d9..67e193e 100644 --- a/src/Infrastructure/MigrationStep/S040FromSourcePimDetectedToDestinationPimDownloaded.php +++ b/src/Infrastructure/MigrationStep/S040FromSourcePimDetectedToDestinationPimDownloaded.php @@ -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(); diff --git a/src/Infrastructure/MigrationStep/S050FromDestinationPimDownloadedToDestinationPimInstalled.php b/src/Infrastructure/MigrationStep/S050FromDestinationPimDownloadedToDestinationPimInstalled.php index 2e74f99..c4e49c3 100644 --- a/src/Infrastructure/MigrationStep/S050FromDestinationPimDownloadedToDestinationPimInstalled.php +++ b/src/Infrastructure/MigrationStep/S050FromDestinationPimDownloadedToDestinationPimInstalled.php @@ -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. diff --git a/src/Infrastructure/TransporteoStateMachine.php b/src/Infrastructure/TransporteoStateMachine.php index eabb4df..e93aabf 100644 --- a/src/Infrastructure/TransporteoStateMachine.php +++ b/src/Infrastructure/TransporteoStateMachine.php @@ -81,6 +81,9 @@ class TransporteoStateMachine /** @var LoggerInterface */ protected $logger; + /** @var array */ + protected $defaultResponses = []; + public function __construct(StateMachine $stateMachine, LoggerInterface $logger) { $this->stateMachineMarker = $stateMachine; @@ -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] ?? ''; + } } diff --git a/src/Infrastructure/UserInterface/Cli/Transporteo.php b/src/Infrastructure/UserInterface/Cli/Transporteo.php index 7a603d3..67d8b05 100644 --- a/src/Infrastructure/UserInterface/Cli/Transporteo.php +++ b/src/Infrastructure/UserInterface/Cli/Transporteo.php @@ -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')); diff --git a/tests/spec/Infrastructure/MigrationStep/S010FromReadyToSourcePimConfiguredSpec.php b/tests/spec/Infrastructure/MigrationStep/S010FromReadyToSourcePimConfiguredSpec.php index 620c903..11c381f 100644 --- a/tests/spec/Infrastructure/MigrationStep/S010FromReadyToSourcePimConfiguredSpec.php +++ b/tests/spec/Infrastructure/MigrationStep/S010FromReadyToSourcePimConfiguredSpec.php @@ -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(); @@ -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? '; @@ -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? '; @@ -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(); diff --git a/tests/spec/Infrastructure/MigrationStep/S015FromSourcePimConfiguredToSourcePimApiConfiguredSpec.php b/tests/spec/Infrastructure/MigrationStep/S015FromSourcePimConfiguredToSourcePimApiConfiguredSpec.php index a57b031..92d7805 100644 --- a/tests/spec/Infrastructure/MigrationStep/S015FromSourcePimConfiguredToSourcePimApiConfiguredSpec.php +++ b/tests/spec/Infrastructure/MigrationStep/S015FromSourcePimConfiguredToSourcePimApiConfiguredSpec.php @@ -47,6 +47,7 @@ public function it_configures_a_source_pim_api( ) { $event->getSubject()->willReturn($stateMachine); + $stateMachine->getDefaultResponse(Argument::any())->willReturn(''); $baseUri = 'http://localhost'; $question = 'What is the base URI to request the API of the source PIM?'; @@ -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, diff --git a/tests/spec/Infrastructure/MigrationStep/S040FromSourcePimDetectedToDestinationPimDownloadedSpec.php b/tests/spec/Infrastructure/MigrationStep/S040FromSourcePimDetectedToDestinationPimDownloadedSpec.php index ff6b101..346503d 100644 --- a/tests/spec/Infrastructure/MigrationStep/S040FromSourcePimDetectedToDestinationPimDownloadedSpec.php +++ b/tests/spec/Infrastructure/MigrationStep/S040FromSourcePimDetectedToDestinationPimDownloadedSpec.php @@ -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); diff --git a/tests/spec/Infrastructure/MigrationStep/S050FromDestinationPimDownloadedToDestinationPimInstalledSpec.php b/tests/spec/Infrastructure/MigrationStep/S050FromDestinationPimDownloadedToDestinationPimInstalledSpec.php index 6718308..2e0ed67 100644 --- a/tests/spec/Infrastructure/MigrationStep/S050FromDestinationPimDownloadedToDestinationPimInstalledSpec.php +++ b/tests/spec/Infrastructure/MigrationStep/S050FromDestinationPimDownloadedToDestinationPimInstalledSpec.php @@ -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://localhost';