CreateDeviceCommand.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 CreateDeviceCommand extends ContainerAwareCommand
  9. {
  10. protected function configure()
  11. {
  12. $this
  13. ->setName('device:create')
  14. ->setDescription('Create Device command')
  15. ->setHelp('Create Device command')
  16. ->addArgument('type', 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 url for device creation')
  19. ->addOption('username', null, InputOption::VALUE_OPTIONAL, 'Username', 'admin')
  20. ->addOption('password', null, InputOption::VALUE_OPTIONAL, 'User password', 'adminpass')
  21. ;
  22. }
  23. /**
  24. * @param InputInterface $input
  25. * @param OutputInterface $output
  26. */
  27. protected function execute(InputInterface $input, OutputInterface $output)
  28. {
  29. $ids = $input->getOption('id');
  30. $url = $input->getOption('url');
  31. $username = $input->getOption('username');
  32. $password = $input->getOption('password');
  33. try {
  34. // busco por device type e id
  35. $container = $this->getContainer();
  36. $em = $container->get('doctrine.orm.entity_manager');
  37. $repository = $em->getRepository($input->getArgument('type'));
  38. $entities = array();
  39. if (!empty($ids)) {
  40. if (!is_array($ids)) {
  41. $ids = array($ids);
  42. }
  43. foreach ($ids as $id) {
  44. if ($entity = $repository->find($id)) {
  45. $entities[] = $entity;
  46. } else {
  47. $output->writeln("Entity id <error>{$id}</error> not found!");
  48. }
  49. }
  50. } else {
  51. $entities = $repository->findAll();
  52. }
  53. // llamada POST para guardar el device
  54. if (!$url && $container->hasParameter('device_post_url')) {
  55. $url = $container->getParameter('device_post_url');
  56. }
  57. $deviceListener = $container->get('device.device_listener');
  58. foreach ($entities as $entity) {
  59. $response = $deviceListener->send($entity, $url, HttpRequestInterface::METHOD_POST, compact('username', 'password'));
  60. if (json_decode($response)) {
  61. $output->writeln('<info>Device created!</info>');
  62. $output->writeln($response);
  63. } else {
  64. $output->writeln("Error: Device for entity id <error>{$id}</error> not created!");
  65. }
  66. }
  67. } catch (\Exception $ex) {
  68. $output->writeln($ex->getMessage());
  69. }
  70. }
  71. }