Quellcode durchsuchen

comando soap:client

Guillermo Espinoza vor 8 Jahren
Ursprung
Commit
996c1b6a41
1 geänderte Dateien mit 105 neuen und 0 gelöschten Zeilen
  1. 105 0
      Command/SOAPClientCommand.php

+ 105 - 0
Command/SOAPClientCommand.php

@@ -0,0 +1,105 @@
+<?php
+
+namespace WorkflowBundle\Command;
+
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class SOAPClientCommand extends Command
+{
+
+    protected function configure()
+    {
+        $this
+            ->setName('soap:client')
+            ->setDescription('Call a SOAP Client Service')
+            ->setHelp('Call a SOAP Client service from wsdl')
+            ->addOption('wsdl', null, InputOption::VALUE_REQUIRED, 'Soap WSDL')
+            ->addOption('service', null, InputOption::VALUE_OPTIONAL, 'Webservice name')
+            ->addOption('filename', null, InputOption::VALUE_OPTIONAL, 'File source of data in json format')
+        ;
+    }
+
+    /**
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     */
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        $wsdl = $input->getOption('wsdl');
+        if (is_null($wsdl)) {
+            $output->writeln("<error>Option --wdsl are required</error>");
+
+            return;
+        }
+
+        try {
+            $soap = new \SoapClient($wsdl);
+
+            $service = $this->getService($input, $output, $soap);
+            $data = $this->getFileData($input, $output);
+
+            $result = $soap->$service($data);
+
+            $output->writeln(PHP_EOL . "Result: {$wsdl} -> {$service}");
+            $output->writeln($result);
+        } catch (\Exception $ex) {
+            $output->writeln(sprintf('<error>%s</error>', $ex->getMessage()));
+        }
+    }
+
+    /**
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     * @param \SoapClient $soap
+     * 
+     * @return string
+     */
+    protected function getService(InputInterface $input, OutputInterface $output, \SoapClient $soap)
+    {
+        $service = $input->getOption('service');
+        $methods = array_map(function ($method) {
+            return explode('(', explode(' ', $method)[1])[0];
+        }, $soap->__getFunctions());
+        while (is_null($service) || !in_array($service, $methods)) {
+            $output->writeln(PHP_EOL . 'Enter one service of the next list:' . PHP_EOL);
+            $output->writeln(implode(', ', $methods));
+            $service = trim(fgets(STDIN));
+            if (!in_array($service, $methods)) {
+                $output->writeln('<error>Invalid Option</error>');
+            }
+        }
+
+        return $service;
+    }
+
+    /**
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     * 
+     * @return array
+     */
+    protected function getFileData(InputInterface $input, OutputInterface $output)
+    {
+        $filename = $input->getOption('filename');
+        while (is_null($filename) || !file_exists($filename)) {
+            $output->writeln(PHP_EOL . 'Enter the source data filename. Press Enter to finish' . PHP_EOL);
+            $stdin = fgets(STDIN);
+            if ($stdin === PHP_EOL) {
+                return;
+            }
+            $filename = trim($stdin);
+            if (!file_exists($filename)) {
+                $output->writeln('<error>File not exists or not found!</error>');
+            }
+        }
+        $data = json_decode(file_get_contents($filename), true);
+        $output->writeln(PHP_EOL . 'Data Recived:');
+        $output->writeln(json_encode($data));
+
+        return $data;
+    }
+
+}