12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace DeviceBundle\Command;
- use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Input\InputOption;
- use Symfony\Component\Console\Output\OutputInterface;
- use Buzz\Message\RequestInterface as HttpRequestInterface;
- class CRUDDeviceCommand extends ContainerAwareCommand
- {
- protected function configure()
- {
- $this
- ->setName('device:crud')
- ->setDescription('CRUD Device command')
- ->setHelp('Create, update or delete a device')
- ->addOption('type', null, InputOption::VALUE_REQUIRED, 'Device Type. e.g. FTTHBundle:ONU, FTTHBundle:OLT')
- ->addOption('id', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Device id. e.g. --id=1 --id=2')
- ->addOption('url', null, InputOption::VALUE_OPTIONAL, 'Device POST | PUT | DELETE url for device creation')
- ->addOption('method', null, InputOption::VALUE_OPTIONAL, 'Http method. e.g. POST, PUT, DELETE', HttpRequestInterface::METHOD_POST)
- ->addOption('username', null, InputOption::VALUE_OPTIONAL, 'Username', 'admin')
- ->addOption('password', null, InputOption::VALUE_OPTIONAL, 'User password', 'adminpass')
- ;
- }
- /**
- * @param InputInterface $input
- * @param OutputInterface $output
- */
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $type = $input->getOption('type');
- $ids = $input->getOption('id');
- $url = $input->getOption('url');
- $method = $input->getOption('method');
- $username = $input->getOption('username');
- $password = $input->getOption('password');
- try {
- // busco por device type e id
- $container = $this->getContainer();
- $em = $container->get('doctrine.orm.entity_manager');
- $repository = $em->getRepository($type);
- $entities = array();
- if (!empty($ids)) {
- if (!is_array($ids)) {
- $ids = array($ids);
- }
- foreach ($ids as $id) {
- if ($entity = $repository->find($id)) {
- $entities[] = $entity;
- } else {
- $output->writeln("Entity {$type} id <error>{$id}</error> not found!");
- }
- }
- } else {
- $entities = $repository->findAll();
- }
- // llamada POST para guardar el device
- if (!$url && $container->hasParameter('device_post_url')) {
- $url = $container->getParameter('device_post_url');
- }
- $deviceListener = $container->get('device.device_listener');
- foreach ($entities as $entity) {
- $response = $deviceListener->send($entity, $url, $method, compact('username', 'password'));
- if (json_decode($response)) {
- if ($method == HttpRequestInterface::METHOD_POST) {
- $output->writeln('<info>Device created!</info>');
- }
- if ($method == HttpRequestInterface::METHOD_PUT) {
- $output->writeln('<info>Device updated!</info>');
- }
- } elseif ($response == '' && $method == HttpRequestInterface::METHOD_DELETE) {
- $output->writeln('<info>Device deleted!</info>');
- } else {
- $output->writeln("Error: Device for entity {$type} id <error>{$id}</error> not created | updated | deleted!");
- }
- $output->writeln($response);
- }
- } catch (\Exception $ex) {
- $output->writeln($ex->getMessage());
- }
- }
- }
|