CRUDDeviceCommand.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. $em->getFilters()->disable('soft_deleteable');
  41. $repository = $em->getRepository($type);
  42. $entities = array();
  43. if (!empty($ids)) {
  44. if (!is_array($ids)) {
  45. $ids = array($ids);
  46. }
  47. foreach ($ids as $id) {
  48. if ($entity = $repository->find($id)) {
  49. $entities[] = $entity;
  50. } else {
  51. $output->writeln("Entity {$type} id <error>{$id}</error> not found!");
  52. }
  53. }
  54. } else {
  55. $entities = $repository->findAll();
  56. }
  57. $em->getFilters()->enable('soft_deleteable');
  58. // llamada POST para guardar el device
  59. if (!$url && $container->hasParameter('device_post_url')) {
  60. $url = $container->getParameter('device_post_url');
  61. }
  62. $deviceListener = $container->get('device.device_listener');
  63. foreach ($entities as $entity) {
  64. $response = $deviceListener->send($entity, $url, $method, compact('username', 'password'));
  65. if (json_decode($response)) {
  66. if ($method == HttpRequestInterface::METHOD_POST) {
  67. $output->writeln('<info>Device created!</info>');
  68. }
  69. if ($method == HttpRequestInterface::METHOD_PUT) {
  70. $output->writeln('<info>Device updated!</info>');
  71. }
  72. } elseif ($response == '' && $method == HttpRequestInterface::METHOD_DELETE) {
  73. $output->writeln('<info>Device deleted!</info>');
  74. } else {
  75. $output->writeln("Error: Device for entity {$type} id <error>{$id}</error> not created | updated | deleted!");
  76. }
  77. $output->writeln($response);
  78. }
  79. } catch (\Exception $ex) {
  80. $output->writeln($ex->getMessage());
  81. }
  82. }
  83. }