Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SimpleCollector.php
Go to the documentation of this file.
1 <?php
8 
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Console\Question\Question;
15 use Symfony\Component\Console\Question\QuestionFactory;
16 use Symfony\Component\Console\Helper\QuestionHelper;
17 
22 {
26  private $questionFactory;
27 
31  private $questionHelper;
32 
37  public function __construct(
38  QuestionFactory $questionFactory,
39  QuestionHelper $questionHelper
40  ) {
41  $this->questionFactory = $questionFactory;
42  $this->questionHelper = $questionHelper;
43  }
44 
54  public function getValues(InputInterface $input, OutputInterface $output, array $configPaths)
55  {
56  $inputPath = $input->getArgument(SensitiveConfigSetCommand::INPUT_ARGUMENT_PATH);
57  $configPathQuestion = $this->getConfigPathQuestion($configPaths);
58  $configPath = ($inputPath === null)
59  ? $this->questionHelper->ask($input, $output, $configPathQuestion)
60  : $inputPath;
61 
62  $this->validatePath($configPath, $configPaths);
63 
64  $inputValue = $input->getArgument(SensitiveConfigSetCommand::INPUT_ARGUMENT_VALUE);
65  $configValueQuestion = $this->getConfigValueQuestion();
66  $configValue = $inputValue === null
67  ? $this->questionHelper->ask($input, $output, $configValueQuestion)
68  : $inputValue;
69 
70  return [$configPath => $configValue];
71  }
72 
79  private function getConfigPathQuestion(array $configPaths)
80  {
82  $configPathQuestion = $this->questionFactory->create([
83  'question' => 'Please enter config path: '
84  ]);
85  $configPathQuestion->setAutocompleterValues($configPaths);
86  $configPathQuestion->setValidator(function ($configPath) use ($configPaths) {
87  $this->validatePath($configPath, $configPaths);
88  return $configPath;
89  });
90 
91  return $configPathQuestion;
92  }
93 
99  private function getConfigValueQuestion()
100  {
102  $configValueQuestion = $this->questionFactory->create([
103  'question' => 'Please enter value: '
104  ]);
105  $configValueQuestion->setValidator(function ($interviewer) {
106  if (empty($interviewer)) {
107  throw new LocalizedException(new Phrase("The value can't be empty. Enter the value and try again."));
108  }
109  return $interviewer;
110  });
111 
112  return $configValueQuestion;
113  }
114 
123  private function validatePath($configPath, array $configPaths)
124  {
125  if (!in_array($configPath, $configPaths)) {
126  throw new LocalizedException(
127  new Phrase('A configuration with this path does not exist or is not sensitive')
128  );
129  }
130  }
131 }
__construct(QuestionFactory $questionFactory, QuestionHelper $questionHelper)
getValues(InputInterface $input, OutputInterface $output, array $configPaths)