setDescription('Send the action at all service');
$this->addArgument('control', InputArgument::REQUIRED, 'The action you want');
$this->addOption(
'service-name',
's',
InputOption::VALUE_REQUIRED,
'Send the controle to the service with service_id. The value must be equal to the configuration.',
self::ALL_SERVICE
);
$this->addOption(
'custom-action',
'c',
InputOption::VALUE_REQUIRED,
'The custom control send to the service.',
null
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$serviceToAction = $input->getOption('service-name');
$customAction = $input->getOption('custom-action');
$action = $input->getArgument('control');
$adminService = new ServiceStateManager();
$actions = ['start', 'stop', 'pause', 'continue', 'custom'];
if (!\in_array($action, $actions)) {
throw new \InvalidArgumentException('The value of action argument is invalid. Valid values : '.implode(', ', $actions));
}
if ($action === 'custom' && ($customAction < 128 || $customAction > 255)) {
throw new \InvalidArgumentException('The custom control value must be between 128 and 255');
}
if ($serviceToAction !== self::ALL_SERVICE) {
$serviceInfos = $this->serviceConfigurationManager->getServiceInformations($serviceToAction);
$this->sendAction($adminService, $action, $serviceInfos, $customAction);
$output->writeln('Sending control to '.$serviceInfos->serviceId().' : OK');
return self::SUCCESS;
}
$nbService = 0;
foreach ($this->serviceConfigurationManager->getFullServiceList() as $serviceInfos) {
try {
++$nbService;
$this->sendAction($adminService, $action, $serviceInfos, $customAction);
$output->writeln('Sending control to '.$serviceInfos->serviceId().' : OK');
} catch (\Exception $e) {
$output->writeln(' Error : '.$serviceInfos->serviceId().'('.$e->getCode().') '.$e->getMessage().' ');
}
}
if ($nbService === 0) {
$output->writeln('No signal sent');
return self::FAILURE;
}
$output->writeln(sprintf('Signal sent to %d service(s)', $nbService));
return self::SUCCESS;
}
private function sendAction(
ServiceStateManager $adminService,
string $action,
ServiceInformations $serviceInfos,
?int $customAction
): void {
switch ($action) {
case 'start':
$adminService->startService($serviceInfos);
break;
case 'stop':
$adminService->stopService($serviceInfos);
break;
case 'pause':
$adminService->pauseService($serviceInfos);
break;
case 'continue':
$adminService->continueService($serviceInfos);
break;
case 'custom':
$adminService->sendCustomControl($serviceInfos, $customAction);
break;
default:
break;
}
}
}