UpdateExtraDataCommand.php 4.3 KB

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