CRUDDeviceCommand.php 4.2 KB

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