UpdateExtraDataCommand.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace ExtraDataBundle\Command;
  3. use Buzz\Client\Curl;
  4. use Buzz\Message\Request as HttpRequest;
  5. use Buzz\Message\RequestInterface as HttpRequestInterface;
  6. use Buzz\Message\Response as HttpResponse;
  7. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  8. use Symfony\Component\Console\Input\InputInterface;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. use Symfony\Component\Console\Input\InputOption;
  11. class UpdateExtraDataCommand extends ContainerAwareCommand
  12. {
  13. protected function configure()
  14. {
  15. $this
  16. ->setName('extradata:update')
  17. ->setDescription('Update entity extradata field')
  18. ->setHelp('Update entity extradata field from remote data')
  19. ->addOption('url', null, InputOption::VALUE_REQUIRED, 'Remote url', 'http://stats.fd3.flowdat.com/api/onus.json')
  20. ->addOption('filter', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Array with filters. e.g., --filter=key1:value1 --filter=key2:value2')
  21. ->addOption('username', null, InputOption::VALUE_REQUIRED, 'Remote username', 'admin')
  22. ->addOption('password', null, InputOption::VALUE_REQUIRED, 'Remote password', 'adminpass')
  23. ->addOption('entity-class', null, InputOption::VALUE_REQUIRED, 'Entity namespace. e.g. FTTHBundle:ONU', 'FTTHBundle:ONU')
  24. ->addOption('id', null, InputOption::VALUE_REQUIRED, 'Entity Id')
  25. ->addOption('fields', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Extradata fields to update')
  26. ;
  27. }
  28. /**
  29. * @param InputInterface $input
  30. * @param OutputInterface $output
  31. *
  32. * @return void
  33. */
  34. protected function execute(InputInterface $input, OutputInterface $output)
  35. {
  36. $url = $input->getOption('url');
  37. $filter = $input->getOption('filter');
  38. $username = $input->getOption('username');
  39. $password = $input->getOption('password');
  40. $entityClass = $input->getOption('entity-class');
  41. $id = $input->getOption('id');
  42. $fields = $input->getOption('fields');
  43. try {
  44. $filters = array();
  45. if (!empty($filter)) {
  46. foreach ($filter as $value) {
  47. $pieces = array_map('trim', explode(':', $value));
  48. if (isset($pieces[0]) && isset($pieces[1]) && $pieces[0] && $pieces[1]) {
  49. $filters[$pieces[0]] = $pieces[1];
  50. }
  51. }
  52. }
  53. $client = new Curl();
  54. $response = new HttpResponse();
  55. $request = new HttpRequest(HttpRequestInterface::METHOD_GET, $url . '?' . http_build_query(array('filters' => $filters)));
  56. $request->addHeader('Authorization: Basic ' . base64_encode($username . ':' . $password));
  57. $client->send($request, $response);
  58. $response = $response->getContent();
  59. $em = $this->getContainer()->get('doctrine')->getEntityManager();
  60. $repository = $em->getRepository($entityClass);
  61. $entity = $repository->find($id);
  62. if (is_null($entity)) {
  63. $output->writeln(sprintf('<error>Entity: %s id: %s not Found!<error>', $entityClass, $entity->getId()));
  64. return;
  65. }
  66. $entityExtraData = $entity->jsonExtraData();
  67. $extradata = json_decode($response, true);
  68. if (!empty($fields)) {
  69. foreach ($fields as $field) {
  70. if (isset($extradata[0][$field])) {
  71. $entityExtraData[$field] = $extradata[0][$field];
  72. }
  73. }
  74. } else {
  75. $entityExtraData = array_merge($entityExtraData, $extradata);
  76. }
  77. $entity->setJsonExtraData($entityExtraData);
  78. $em->flush($entity);
  79. $output->writeln(sprintf('Entity: %s id: %s updated!', $entityClass, $entity->getId()));
  80. } catch (\Exception $e) {
  81. var_dump($e->getMessage());
  82. die;
  83. }
  84. }
  85. }