CRUDDeviceCommand.php 4.2 KB

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