Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
mitelg committed May 15, 2018
1 parent 0f7562c commit 02bcf9d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
3 changes: 1 addition & 2 deletions src/Extensions/Shopware/Plugin/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ private function populateContainer($container)
$container->register('plugin_operation_manager', 'Shopware\Plugin\Services\ConsoleInteraction\PluginOperationManager')
->addArgument(new Reference('plugin_provider'))
->addArgument(new Reference('plugin_selector'))
->addArgument(new Reference('io_service'))
->addArgument(new Reference('utilities'));
->addArgument(new Reference('io_service'));

$container->register('bootstrap_info', 'Shopware\Plugin\Services\BootstrapInfo');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(IoService $ioService, PluginColumnRenderer $outputRe
* @param string[] $allowedAnswers
* @return string
*/
public function selectPlugin($plugins, $allowedAnswers = ['all'])
public function selectPlugin($plugins, array $allowedAnswers = ['all'])
{
while (true) {
system('clear');
Expand All @@ -66,6 +66,10 @@ public function selectPlugin($plugins, $allowedAnswers = ['all'])

$response = $this->ioService->ask($question);

if (in_array($response, $allowedAnswers, true)) {
return $response;
}

if ($range = $this->getPluginRange($response)) {
return array_filter(
array_map(
Expand All @@ -78,14 +82,14 @@ function ($plugin) {
return $plugin;
}
);
} elseif (isset($plugins[$response - 1])) {
}

if (isset($plugins[$response - 1])) {
return $plugins[$response - 1];
} elseif (in_array($response, $allowedAnswers)) {
return $response;
} else {
$question = new Question('<error>Invalid answer, hit enter to continue</error>');
$this->ioService->ask($question);
}

$question = new Question('<error>Invalid answer, hit enter to continue</error>');
$this->ioService->ask($question);
}
}

Expand All @@ -102,18 +106,20 @@ private function formatQuestion($count, $allowedAnswers)

if (empty($allowedAnswers)) {
return sprintf($template, '');
} elseif (count($allowedAnswers) == 1) {
return sprintf($template, sprintf('or "%s"', $allowedAnswers[0]));
} else {
$allowedAnswers = array_map(
function ($option) {
return sprintf('"<comment>%s</comment>"', $option);
},
$allowedAnswers
);
}

return sprintf($template, sprintf('or one of these: %s', implode(', ', $allowedAnswers)));
if (count($allowedAnswers) === 1) {
return sprintf($template, sprintf('or "%s"', $allowedAnswers[0]));
}

$allowedAnswers = array_map(
function ($option) {
return sprintf('"<comment>%s</comment>"', $option);
},
$allowedAnswers
);

return sprintf($template, sprintf('or one of these: %s', implode(', ', $allowedAnswers)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ class PluginOperationManager
*/
protected $output;

/**
* @var Utilities
*/
private $utilities;

/**
* @var PluginInputVerificator
*/
Expand All @@ -50,17 +45,14 @@ class PluginOperationManager
* @param PluginProvider $pluginProvider
* @param PluginInputVerificator $pluginSelector
* @param IoService $ioService
* @param Utilities $utilities
*/
public function __construct(
PluginProvider $pluginProvider,
PluginInputVerificator $pluginSelector,
IoService $ioService,
Utilities $utilities
IoService $ioService
) {
$this->pluginProvider = $pluginProvider;
$this->pluginSelector = $pluginSelector;
$this->utilities = $utilities;
$this->ioService = $ioService;
}

Expand All @@ -78,19 +70,19 @@ public function searchAndOperate($names, $callback, $params)
$plugins = $this->pluginProvider->getPluginByName($name);
$count = count($plugins);

if ($count == 0) {
if ($count === 0) {
$plugins = $this->pluginProvider->getPluginsByRepositoryName($name);
$count = count($plugins);
}
if ($count == 0) {
if ($count === 0) {
$this->ioService->writeln("\n<error>Could not find a plugin named '{$name}'</error>");

return;
}

$this->ioService->writeln("\nWill now process '<comment>{$name}</comment>'");

if ($count == 1) {
if ($count === 1) {
$this->executeMethodCallback($plugins[0], $callback, $params);

return;
Expand Down Expand Up @@ -126,9 +118,13 @@ private function getPluginsFromResponse($response, $plugins)
{
if ($response instanceof Plugin) {
return [$response];
} elseif (is_array($response)) {
}

if (is_array($response)) {
return $response;
} elseif ($response == 'all') {
}

if ($response === 'all') {
return $plugins;
}
}
Expand All @@ -145,7 +141,7 @@ public function operationLoop($callback, $params)
while (true) {
$response = $this->pluginSelector->selectPlugin($plugins, ['all', 'exit']);

if ($response == 'exit') {
if ($response === 'exit') {
return;
}

Expand Down

0 comments on commit 02bcf9d

Please sign in to comment.