CRUDDeviceCommand.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. use Base\AdminBundle\Interfaces\SoftDeleteInterface;
  9. class CRUDDeviceCommand extends ContainerAwareCommand
  10. {
  11. protected function configure()
  12. {
  13. $params = array('CMD_USERNAME' => getenv("CMD_USERNAME")?getenv("CMD_USERNAME"):'admin' , 'CMD_PASSWORD' => getenv("CMD_PASSWORD")?getenv("CMD_PASSWORD"):'adminpass');
  14. $this
  15. ->setName('device:crud')
  16. ->setDescription('CRUD Device command')
  17. ->setHelp('Create, update or delete a device')
  18. ->addOption('type', null, InputOption::VALUE_REQUIRED, 'Device Type. e.g. FTTHBundle:ONU, FTTHBundle:OLT')
  19. ->addOption('id', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Device id. e.g. --id=1 --id=2')
  20. ->addOption('url', null, InputOption::VALUE_OPTIONAL, 'Device POST | PUT | DELETE url for device creation')
  21. ->addOption('method', null, InputOption::VALUE_OPTIONAL, 'Http method. e.g. POST, PUT, DELETE', HttpRequestInterface::METHOD_POST)
  22. ->addOption('username', null, InputOption::VALUE_OPTIONAL, 'Username or specify CMD_USERNAME enviroment variable', $params["CMD_USERNAME"])
  23. ->addOption('password', null, InputOption::VALUE_OPTIONAL, 'User password or specify CMD_PASSWORD enviroment variable', $params["CMD_PASSWORD"])
  24. ;
  25. }
  26. /**
  27. * @param InputInterface $input
  28. * @param OutputInterface $output
  29. */
  30. protected function execute(InputInterface $input, OutputInterface $output)
  31. {
  32. $type = $input->getOption('type');
  33. $ids = $input->getOption('id');
  34. $url = $input->getOption('url');
  35. $method = $input->getOption('method');
  36. $username = $input->getOption('username');
  37. $password = $input->getOption('password');
  38. $enabled = false;
  39. try {
  40. // busco por device type e id
  41. $container = $this->getContainer();
  42. $em = $container->get('doctrine.orm.entity_manager');
  43. //$em->getFilters()->disable('soft_deleteable');
  44. if($em->getFilters()->isEnabled('soft_deleteable')) {
  45. $enabled = true;
  46. $em->getFilters()->disable('soft_deleteable');
  47. }
  48. $repository = $em->getRepository($type);
  49. $entities = array();
  50. if (!empty($ids)) {
  51. if (!is_array($ids)) {
  52. $ids = array($ids);
  53. }
  54. foreach ($ids as $id) {
  55. if ($entity = $repository->find($id)) {
  56. $entities[] = $entity;
  57. } else {
  58. $output->writeln("Entity {$type} id <error>{$id}</error> not found!");
  59. }
  60. }
  61. } else {
  62. $entities = $repository->findAll();
  63. }
  64. if($enabled) {
  65. $em->getFilters()->enable('soft_deleteable');
  66. }
  67. // llamada POST para guardar el device
  68. if (!$url && $container->hasParameter('device_post_url')) {
  69. $url = $container->getParameter('device_post_url');
  70. }
  71. $deviceListener = $container->get('device.device_listener');
  72. foreach ($entities as $entity) {
  73. $response = $deviceListener->send($entity, $url, $method, compact('username', 'password'));
  74. if (json_decode($response)) {
  75. if ($method == HttpRequestInterface::METHOD_POST) {
  76. $output->writeln('<info>Device created!</info>');
  77. }
  78. if ($method == HttpRequestInterface::METHOD_PUT) {
  79. $output->writeln('<info>Device updated!</info>');
  80. }
  81. } elseif ($response == '' && $method == HttpRequestInterface::METHOD_DELETE) {
  82. $output->writeln('<info>Device deleted!</info>');
  83. } else {
  84. $output->writeln("Error: Device for entity {$type} id <error>{$id}</error> not created | updated | deleted!");
  85. }
  86. $output->writeln($response);
  87. }
  88. } catch (\Exception $ex) {
  89. $output->writeln($ex->getMessage());
  90. }
  91. }
  92. }