I need refactoring this function with SOLID or clean code or design patterns, any can help me? The switch I don't know if I can refactoring
I have created new functions for decoupling but switch loop it's driving me crazy
I added custom functions created by me, hasCorrectSugars check that number of sugars is 0,1 or 2 and checkSugars check that exists sugars...
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->setDrinkType($input);
if (in_array($this->drinkType, $this->allowedDrinkTypes)) {
/**
* Tea --> 0.4
* Coffee --> 0.5
* Chocolate --> 0.6
*/
$money = $input->getArgument('money');
switch ($this->drinkType) {
case 'tea':
if ($money < 0.4) {
$output->writeln('The tea costs 0.4');
return 0;
}
break;
case 'coffee':
if ($money < 0.5) {
$output->writeln('The coffee costs 0.5');
return 0;
}
break;
case 'chocolate':
if ($money < 0.6) {
$output->writeln('The chocolate costs 0.6');
return 0;
}
break;
}
if ($this->hasCorrectSugars($input)) {
$this->checkSugars($input, $output);
return 0;
}
$output->writeln('The number of sugars should be between 0 and 2');
return 0;
}
$output->writeln('The drink type should be tea, coffee or chocolate');
return 0;
}
protected function hasCorrectSugars($input): bool
{
$sugars = $input->getArgument('sugars');
if ($sugars >= $this->minSugars && $sugars <= $this->maxSugars) {
return true;
}
return false;
}
protected function checkSugars($input, $output): void
{
$sugars = $input->getArgument('sugars');
$output->write('You have ordered a ' . $this->drinkType);
$this->isExtraHot($input, $output);
$output->write(' with ' . $sugars . ' sugars');
if ($sugars > 0) {
$output->write(' (stick included)');
}
$output->writeln('');
}
Aucun commentaire:
Enregistrer un commentaire