CRUDDeviceCommand.php 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace DeviceBundle\Command;
  3. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Input\InputOption;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use Buzz\Message\RequestInterface as HttpRequestInterface;
  8. class CRUDDeviceCommand extends ContainerAwareCommand
  9. {
  10. protected function configure()
  11. {
  12. $this
  13. ->setName('device:crud')
  14. ->setDescription('CRUD Device command')
  15. ->setHelp('Create, update or delete a device')
  16. ->addOption('type', null, InputOption::VALUE_REQUIRED, 'Device Type. e.g. FTTHBundle:ONU, FTTHBundle:OLT')
  17. ->addOption('id', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Device id. e.g. --id=1 --id=2')
  18. ->addOption('url', null, InputOption::VALUE_OPTIONAL, 'Device POST | PUT | DELETE url for device creation')
  19. ->addOption('method', null, InputOption::VALUE_OPTIONAL, 'Http method. e.g. POST, PUT, DELETE', HttpRequestInterface::METHOD_POST)
  20. ->addOption('username', null, InputOption::VALUE_OPTIONAL, 'Username', 'admin')
  21. ->addOption('password', null, InputOption::VALUE_OPTIONAL, 'User password', 'adminpass')
  22. ;
  23. }
  24. /**
  25. * @param InputInterface $input
  26. * @param OutputInterface $output
  27. */
  28. protected function execute(InputInterface $input, OutputInterface $output)
  29. {
  30. $type = $input->getOption('type');
  31. $ids = $input->getOption('id');
  32. $url = $input->getOption('url');
  33. $method = $input->getOption('method');
  34. $username = $input->getOption('username');
  35. $password = $input->getOption('password');
  36. try {
  37. // busco por device type e id
  38. $container = $this->getContainer();
  39. $em = $container->get('doctrine.orm.entity_manager');
  40. $repository = $em->getRepository($type);
  41. $entities = array();
  42. if (!empty($ids)) {
  43. if (!is_array($ids)) {
  44. $ids = array($ids);
  45. }
  46. foreach ($ids as $id) {
  47. if ($entity = $repository->find($id)) {
  48. $entities[] = $entity;
  49. } else {
  50. $output->writeln("Entity {$type} id <error>{$id}</error> not found!");
  51. }
  52. }
  53. } else {
  54. $entities = $repository->findAll();
  55. }
  56. // llamada POST para guardar el device
  57. if (!$url && $container->hasParameter('device_post_url')) {
  58. $url = $container->getParameter('device_post_url');
  59. }
  60. $deviceListener = $container->get('device.device_listener');
  61. foreach ($entities as $entity) {
  62. $response = $deviceListener->send($entity, $url, $method, compact('username', 'password'));
  63. if (json_decode($response)) {
  64. if ($method == HttpRequestInterface::METHOD_POST) {
  65. $output->writeln('<info>Device created!</info>');
  66. }
  67. if ($method == HttpRequestInterface::METHOD_PUT) {
  68. $output->writeln('<info>Device updated!</info>');
  69. }
  70. } elseif ($response == '' && $method == HttpRequestInterface::METHOD_DELETE) {
  71. $output->writeln('<info>Device deleted!</info>');
  72. } else {
  73. $output->writeln("Error: Device for entity {$type} id <error>{$id}</error> not created | updated | deleted!");
  74. }
  75. $output->writeln($response);
  76. }
  77. } catch (\Exception $ex) {
  78. $output->writeln($ex->getMessage());
  79. }
  80. }
  81. }