setName('device:create') ->setDescription('Create Device command') ->setHelp('Create Device command') ->addArgument('type', 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 url for device creation') ->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) { $ids = $input->getOption('id'); $url = $input->getOption('url'); $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($input->getArgument('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 id {$id} 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, HttpRequestInterface::METHOD_POST, compact('username', 'password')); if (json_decode($response)) { $output->writeln('Device created!'); $output->writeln($response); } else { $output->writeln("Error: Device for entity id {$id} not created!"); } } } catch (\Exception $ex) { $output->writeln($ex->getMessage()); } } }