123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?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;
- use Base\AdminBundle\Interfaces\SoftDeleteInterface;
- class CRUDDeviceCommand extends ContainerAwareCommand
- {
- protected function configure()
- {
- $params = array('CMD_USERNAME' => getenv("CMD_USERNAME")?getenv("CMD_USERNAME"):'admin' , 'CMD_PASSWORD' => getenv("CMD_PASSWORD")?getenv("CMD_PASSWORD"):'adminpass');
- $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 or specify CMD_USERNAME enviroment variable', $params["CMD_USERNAME"])
- ->addOption('password', null, InputOption::VALUE_OPTIONAL, 'User password or specify CMD_PASSWORD enviroment variable', $params["CMD_PASSWORD"])
- ;
- }
- /**
- * @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');
- $enabled = false;
- try {
- // busco por device type e id
- $container = $this->getContainer();
- $em = $container->get('doctrine.orm.entity_manager');
- //$em->getFilters()->disable('soft_deleteable');
- if($em->getFilters()->isEnabled('soft_deleteable')) {
- $enabled = true;
- $em->getFilters()->disable('soft_deleteable');
- }
- $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();
- }
- if($enabled) {
- $em->getFilters()->enable('soft_deleteable');
- }
- // 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());
- }
- }
- }
|