|
@@ -8,18 +8,19 @@ use Symfony\Component\Console\Input\InputOption;
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
use Buzz\Message\RequestInterface as HttpRequestInterface;
|
|
|
|
|
|
-class CreateDeviceCommand extends ContainerAwareCommand
|
|
|
+class CRUDDeviceCommand extends ContainerAwareCommand
|
|
|
{
|
|
|
|
|
|
protected function configure()
|
|
|
{
|
|
|
$this
|
|
|
- ->setName('device:create')
|
|
|
- ->setDescription('Create Device command')
|
|
|
- ->setHelp('Create Device command')
|
|
|
+ ->setName('device:crud')
|
|
|
+ ->setDescription('CRUD Device command')
|
|
|
+ ->setHelp('Create, update or delete a device')
|
|
|
->addArgument('type', InputOption::VALUE_REQUIRED, 'Device Type. e.g. FTTHBundle:ONU, FTTHBundle:OLT')
|
|
|
->addOption('id', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Device id. e.g. --id=1 --id=2')
|
|
|
- ->addOption('url', null, InputOption::VALUE_OPTIONAL, 'Device POST url for device creation')
|
|
|
+ ->addOption('url', null, InputOption::VALUE_OPTIONAL, 'Device POST | PUT | DELETE url for device creation')
|
|
|
+ ->addOption('method', null, InputOption::VALUE_OPTIONAL, 'Http method. e.g. POST, PUT, DELETE', HttpRequestInterface::METHOD_POST)
|
|
|
->addOption('username', null, InputOption::VALUE_OPTIONAL, 'Username', 'admin')
|
|
|
->addOption('password', null, InputOption::VALUE_OPTIONAL, 'User password', 'adminpass')
|
|
|
;
|
|
@@ -31,15 +32,17 @@ class CreateDeviceCommand extends ContainerAwareCommand
|
|
|
*/
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
{
|
|
|
+ $type = $input->getArgument('type');
|
|
|
$ids = $input->getOption('id');
|
|
|
$url = $input->getOption('url');
|
|
|
+ $method = $input->getOption('method');
|
|
|
$username = $input->getOption('username');
|
|
|
$password = $input->getOption('password');
|
|
|
try {
|
|
|
// busco por device type e id
|
|
|
$container = $this->getContainer();
|
|
|
$em = $container->get('doctrine.orm.entity_manager');
|
|
|
- $repository = $em->getRepository($input->getArgument('type'));
|
|
|
+ $repository = $em->getRepository($type);
|
|
|
$entities = array();
|
|
|
if (!empty($ids)) {
|
|
|
if (!is_array($ids)) {
|
|
@@ -49,7 +52,7 @@ class CreateDeviceCommand extends ContainerAwareCommand
|
|
|
if ($entity = $repository->find($id)) {
|
|
|
$entities[] = $entity;
|
|
|
} else {
|
|
|
- $output->writeln("Entity id <error>{$id}</error> not found!");
|
|
|
+ $output->writeln("Entity {$type} id <error>{$id}</error> not found!");
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
@@ -62,13 +65,20 @@ class CreateDeviceCommand extends ContainerAwareCommand
|
|
|
}
|
|
|
$deviceListener = $container->get('device.device_listener');
|
|
|
foreach ($entities as $entity) {
|
|
|
- $response = $deviceListener->send($entity, $url, HttpRequestInterface::METHOD_POST, compact('username', 'password'));
|
|
|
+ $response = $deviceListener->send($entity, $url, $method, compact('username', 'password'));
|
|
|
if (json_decode($response)) {
|
|
|
- $output->writeln('<info>Device created!</info>');
|
|
|
- $output->writeln($response);
|
|
|
+ if ($method == HttpRequestInterface::METHOD_POST) {
|
|
|
+ $output->writeln('<info>Device created!</info>');
|
|
|
+ }
|
|
|
+ if ($method == HttpRequestInterface::METHOD_PUT) {
|
|
|
+ $output->writeln('<info>Device updated!</info>');
|
|
|
+ }
|
|
|
+ } elseif ($response == '' && $method == HttpRequestInterface::METHOD_DELETE) {
|
|
|
+ $output->writeln('<info>Device deleted!</info>');
|
|
|
} else {
|
|
|
- $output->writeln("Error: Device for entity id <error>{$id}</error> not created!");
|
|
|
+ $output->writeln("Error: Device for entity {$type} id <error>{$id}</error> not created | updated | deleted!");
|
|
|
}
|
|
|
+ $output->writeln($response);
|
|
|
}
|
|
|
} catch (\Exception $ex) {
|
|
|
$output->writeln($ex->getMessage());
|