These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace N98\Util\Console\Helper; |
||
4 | |||
5 | use Exception; |
||
6 | use InvalidArgumentException; |
||
7 | use N98\Util\Validator\FakeMetadataFactory; |
||
8 | use RuntimeException; |
||
9 | use Symfony\Component\Console\Helper\Helper as AbstractHelper; |
||
10 | use Symfony\Component\Console\Output\OutputInterface; |
||
11 | use Symfony\Component\Console\Input\InputInterface; |
||
12 | use Symfony\Component\Translation\Translator; |
||
13 | use Symfony\Component\Validator\Constraint; |
||
14 | use Symfony\Component\Validator\Validator; |
||
15 | use Symfony\Component\Validator\Constraints; |
||
16 | use Symfony\Component\Validator\ConstraintValidatorFactory; |
||
17 | |||
18 | /** |
||
19 | * Helper to init some parameters |
||
20 | */ |
||
21 | class ParameterHelper extends AbstractHelper |
||
22 | { |
||
23 | /** |
||
24 | * @var Validator |
||
25 | */ |
||
26 | protected $validator; |
||
27 | |||
28 | /** |
||
29 | * Returns the canonical name of this helper. |
||
30 | * |
||
31 | * @return string The canonical name |
||
32 | * |
||
33 | * @api |
||
34 | */ |
||
35 | public function getName() |
||
36 | { |
||
37 | return 'parameter'; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @param InputInterface $input |
||
42 | * @param OutputInterface $output |
||
43 | * @param string $argumentName |
||
44 | * @param bool $withDefaultStore [optional] |
||
45 | * |
||
46 | * @return mixed |
||
47 | * |
||
48 | * @throws InvalidArgumentException |
||
49 | */ |
||
50 | View Code Duplication | public function askStore(InputInterface $input, OutputInterface $output, $argumentName = 'store', $withDefaultStore = false) |
|
0 ignored issues
–
show
|
|||
51 | { |
||
52 | /* @var $storeManager \Mage_Core_Model_App */ |
||
53 | $storeManager = \Mage::app(); |
||
54 | |||
55 | try { |
||
56 | if ($input->getArgument($argumentName) === null) { |
||
57 | throw new RuntimeException('No store given'); |
||
58 | } |
||
59 | /** @var $store \Mage_Core_Model_Store */ |
||
60 | $store = $storeManager->getStore($input->getArgument($argumentName)); |
||
61 | } catch (Exception $e) { |
||
62 | $stores = array(); |
||
63 | $i = 0; |
||
64 | |||
65 | foreach ($storeManager->getStores($withDefaultStore) as $store) { |
||
66 | $stores[$i] = $store->getId(); |
||
67 | $question[] = '<comment>[' . ($i + 1) . ']</comment> ' . $store->getCode() . ' - ' . $store->getName() . PHP_EOL; |
||
68 | $i++; |
||
69 | } |
||
70 | |||
71 | if (count($stores) > 1) { |
||
72 | $question[] = '<question>Please select a store: </question>'; |
||
73 | $storeId = $this->getHelperSet()->get('dialog')->askAndValidate($output, $question, function($typeInput) use ($stores) { |
||
0 ignored issues
–
show
|
|||
74 | if (!isset($stores[$typeInput - 1])) { |
||
75 | throw new InvalidArgumentException('Invalid store'); |
||
76 | } |
||
77 | |||
78 | return $stores[$typeInput - 1]; |
||
79 | }); |
||
80 | } else { |
||
81 | // only one store view available -> take it |
||
82 | $storeId = $stores[0]; |
||
83 | } |
||
84 | |||
85 | $store = $storeManager->getStore($storeId); |
||
86 | } |
||
87 | |||
88 | return $store; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param InputInterface $input |
||
93 | * @param OutputInterface $output |
||
94 | * @param string $argumentName |
||
95 | * |
||
96 | * @return mixed |
||
97 | * @throws InvalidArgumentException |
||
98 | * @throws RuntimeException |
||
99 | */ |
||
100 | View Code Duplication | public function askWebsite(InputInterface $input, OutputInterface $output, $argumentName = 'website') |
|
101 | { |
||
102 | /* @var $storeManager \Mage_Core_Model_App */ |
||
103 | $storeManager = \Mage::app(); |
||
104 | |||
105 | try { |
||
106 | if ($input->getArgument($argumentName) === null) { |
||
107 | throw new RuntimeException('No website given'); |
||
108 | } |
||
109 | /** @var $website \Mage_Core_Model_Website */ |
||
110 | $website = $storeManager->getWebsite($input->getArgument($argumentName)); |
||
111 | } catch (Exception $e) { |
||
112 | $i = 0; |
||
113 | $websites = array(); |
||
114 | foreach ($storeManager->getWebsites() as $website) { |
||
115 | $websites[$i] = $website->getId(); |
||
116 | $question[] = '<comment>[' . ($i + 1) . ']</comment> ' . $website->getCode() . ' - ' . $website->getName() . PHP_EOL; |
||
117 | $i++; |
||
118 | } |
||
119 | if (count($websites) == 1) { |
||
120 | return $storeManager->getWebsite($websites[0]); |
||
121 | } |
||
122 | $question[] = '<question>Please select a website: </question>'; |
||
123 | |||
124 | $websiteId = $this->getHelperSet()->get('dialog')->askAndValidate($output, $question, function($typeInput) use ($websites) { |
||
0 ignored issues
–
show
|
|||
125 | if (!isset($websites[$typeInput - 1])) { |
||
126 | throw new InvalidArgumentException('Invalid store'); |
||
127 | } |
||
128 | |||
129 | return $websites[$typeInput - 1]; |
||
130 | }); |
||
131 | |||
132 | $website = $storeManager->getWebsite($websiteId); |
||
133 | } |
||
134 | |||
135 | return $website; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param InputInterface $input |
||
140 | * @param OutputInterface $output |
||
141 | * @param string $argumentName |
||
142 | * |
||
143 | * @return string |
||
144 | */ |
||
145 | public function askEmail(InputInterface $input, OutputInterface $output, $argumentName = 'email') |
||
146 | { |
||
147 | $constraints = new Constraints\Collection( |
||
148 | array( |
||
149 | 'email' => array( |
||
150 | new Constraints\NotBlank(), |
||
151 | new Constraints\Email() |
||
152 | ) |
||
153 | ) |
||
154 | ); |
||
155 | |||
156 | return $this->_validateArgument($output, $argumentName, $input->getArgument($argumentName), $constraints); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param InputInterface $input |
||
161 | * @param OutputInterface $output |
||
162 | * @param string $argumentName |
||
163 | * |
||
164 | *@return string |
||
165 | */ |
||
166 | public function askPassword( |
||
167 | InputInterface $input, |
||
168 | OutputInterface $output, |
||
169 | $argumentName = 'password', |
||
170 | $needDigits = true |
||
171 | ) |
||
172 | { |
||
173 | $validators = array(); |
||
174 | |||
175 | if ($needDigits) { |
||
176 | $regex = array( |
||
177 | 'pattern' => '/^(?=.*\d)(?=.*[a-zA-Z])/', |
||
178 | 'message' => 'Password must contain letters and at least one digit' |
||
179 | ); |
||
180 | $validators[] = new Constraints\Regex($regex); |
||
181 | } |
||
182 | |||
183 | $validators[] = new Constraints\Length(array('min' => 6)); |
||
184 | |||
185 | $constraints = new Constraints\Collection( |
||
186 | array( |
||
187 | 'password' => $validators |
||
188 | ) |
||
189 | ); |
||
190 | |||
191 | return $this->_validateArgument($output, $argumentName, $input->getArgument($argumentName), $constraints); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @param OutputInterface $output |
||
196 | * @param string $name |
||
197 | * @param string $value |
||
198 | * @param Constraints\Collection|Constraint|Constraint[] $constraints The constraint(s) to validate against. |
||
199 | * |
||
200 | * @return mixed |
||
201 | */ |
||
202 | protected function _validateArgument(OutputInterface $output, $name, $value, $constraints) |
||
203 | { |
||
204 | $validator = $this->initValidator(); |
||
205 | $errors = null; |
||
206 | |||
207 | if (!empty($value)) { |
||
208 | $errors = $validator->validateValue(array($name => $value), $constraints); |
||
209 | if (count($errors) > 0) { |
||
210 | $output->writeln('<error>' . $errors[0]->getMessage() . '</error>'); |
||
211 | } |
||
212 | } |
||
213 | |||
214 | if (count($errors) > 0 || empty($value)) { |
||
215 | $question = '<question>' . ucfirst($name) . ': </question>'; |
||
216 | $value = $this->getHelperSet()->get('dialog')->askAndValidate( |
||
217 | $output, |
||
218 | $question, |
||
219 | function($typeInput) use ($validator, $constraints, $name) { |
||
220 | $errors = $validator->validateValue(array($name => $typeInput), $constraints); |
||
221 | if (count($errors) > 0) { |
||
222 | throw new InvalidArgumentException($errors[0]->getMessage()); |
||
223 | } |
||
224 | |||
225 | return $typeInput; |
||
226 | } |
||
227 | ); |
||
228 | return $value; |
||
229 | } |
||
230 | return $value; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @return Validator |
||
235 | */ |
||
236 | protected function initValidator() |
||
237 | { |
||
238 | if ($this->validator == null) { |
||
239 | $factory = new ConstraintValidatorFactory(); |
||
240 | $this->validator = new Validator(new FakeMetadataFactory(), $factory, new Translator('en')); |
||
241 | } |
||
242 | |||
243 | return $this->validator; |
||
244 | } |
||
245 | } |
||
246 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.