SOAPClientCommand.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace WorkflowBundle\Command;
  3. use Symfony\Component\Console\Command\Command;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Input\InputOption;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. class SOAPClientCommand extends Command
  8. {
  9. protected function configure()
  10. {
  11. $this
  12. ->setName('soap:client')
  13. ->setDescription('Call a SOAP Client Service')
  14. ->setHelp('Call a SOAP Client service from wsdl')
  15. ->addOption('wsdl', null, InputOption::VALUE_REQUIRED, 'Soap WSDL')
  16. ->addOption('service', null, InputOption::VALUE_OPTIONAL, 'Webservice name')
  17. ->addOption('filename', null, InputOption::VALUE_OPTIONAL, 'File source of data in json format')
  18. ;
  19. }
  20. /**
  21. * @param InputInterface $input
  22. * @param OutputInterface $output
  23. */
  24. protected function execute(InputInterface $input, OutputInterface $output)
  25. {
  26. $wsdl = $input->getOption('wsdl');
  27. if (is_null($wsdl)) {
  28. $output->writeln("<error>Option --wdsl are required</error>");
  29. return;
  30. }
  31. try {
  32. $soap = new \SoapClient($wsdl);
  33. $service = $this->getService($input, $output, $soap);
  34. $data = $this->getFileData($input, $output);
  35. $result = $soap->$service($data);
  36. $output->writeln(PHP_EOL . "Result: {$wsdl} -> {$service}");
  37. $output->writeln($result);
  38. } catch (\Exception $ex) {
  39. $output->writeln(sprintf('<error>%s</error>', $ex->getMessage()));
  40. }
  41. }
  42. /**
  43. * @param InputInterface $input
  44. * @param OutputInterface $output
  45. * @param \SoapClient $soap
  46. *
  47. * @return string
  48. */
  49. protected function getService(InputInterface $input, OutputInterface $output, \SoapClient $soap)
  50. {
  51. $service = $input->getOption('service');
  52. $methods = array_map(function ($method) {
  53. return explode('(', explode(' ', $method)[1])[0];
  54. }, $soap->__getFunctions());
  55. while (is_null($service) || !in_array($service, $methods)) {
  56. $output->writeln(PHP_EOL . 'Enter one service of the next list:' . PHP_EOL);
  57. $output->writeln(implode(', ', $methods));
  58. $service = trim(fgets(STDIN));
  59. if (!in_array($service, $methods)) {
  60. $output->writeln('<error>Invalid Option</error>');
  61. }
  62. }
  63. return $service;
  64. }
  65. /**
  66. * @param InputInterface $input
  67. * @param OutputInterface $output
  68. *
  69. * @return array
  70. */
  71. protected function getFileData(InputInterface $input, OutputInterface $output)
  72. {
  73. $filename = $input->getOption('filename');
  74. while (is_null($filename) || !file_exists($filename)) {
  75. $output->writeln(PHP_EOL . 'Enter the source data filename. Press Enter to finish' . PHP_EOL);
  76. $stdin = fgets(STDIN);
  77. if ($stdin === PHP_EOL) {
  78. return;
  79. }
  80. $filename = trim($stdin);
  81. if (!file_exists($filename)) {
  82. $output->writeln('<error>File not exists or not found!</error>');
  83. }
  84. }
  85. $data = json_decode(file_get_contents($filename), true);
  86. $output->writeln(PHP_EOL . 'Data Recived:');
  87. $output->writeln(json_encode($data));
  88. return $data;
  89. }
  90. }