src/WsProxy/Subscriber/SoapCallCollectorSubscriber.php line 67

Open in your IDE?
  1. <?php
  2. namespace App\WsProxy\Subscriber;
  3. use App\Ws\SoapClient\Transport;
  4. use App\WsProxy\Subscriber\Type\SoapCall;
  5. use Phpro\SoapClient\Event\RequestEvent;
  6. use Phpro\SoapClient\Event\ResponseEvent;
  7. use Soap\ExtSoapEngine\Transport\TraceableTransport;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Stopwatch\Stopwatch;
  12. use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector;
  13. use Throwable;
  14. use function count;
  15. use function strlen;
  16. /**
  17.  * Class SoapCallCollector
  18.  * @package Phpro\SoapClient\BridgeBundle\SoapCallCollector
  19.  */
  20. class SoapCallCollectorSubscriber extends AbstractDataCollector implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * Used for timing the requests
  24.      * @var Stopwatch
  25.      */
  26.     protected $stopwatch;
  27.     /**
  28.      * Collection of requests
  29.      * @var array
  30.      */
  31.     protected $data = [
  32.         'calls' => []
  33.     ];
  34.     /**
  35.      * @var Transport
  36.      */
  37.     protected Transport $transport;
  38.     public function __construct(
  39.         ?Stopwatch $stopwatch
  40.     )
  41.     {
  42.         $this->setStopwatch($stopwatch);
  43.         $this->transport = new Transport();
  44.     }
  45.     /**
  46.      * {@inheritdoc}
  47.      */
  48.     public static function getSubscribedEvents(): array
  49.     {
  50.         return [
  51.             ResponseEvent::class => 'onClientResponse',
  52.             RequestEvent::class => 'onClientRequest'
  53.         ];
  54.     }
  55.     /**
  56.      * Process client request event
  57.      * @param RequestEvent $e
  58.      */
  59.     public function onClientRequest(RequestEvent $e): void
  60.     {
  61.         $call = new SoapCall($this->stopwatch);
  62.         $call->setRequest($e);
  63.         $this->data['calls'][] = $call;
  64.     }
  65.     /**
  66.      * Process client response event
  67.      * @param ResponseEvent $e
  68.      */
  69.     public function onClientResponse(ResponseEvent $e): void
  70.     {
  71.         $responseClassname get_class($e->getResponse());
  72.         $transport $this->transport->getTransport($this->transport->getClient($responseClassname));
  73.         /** @var SoapCall $call */
  74.         $call array_pop($this->data['calls']);
  75.         $call->setResponse($e$transport$this->transport->getClientClassName($responseClassname));
  76.         $this->data['calls'][] = $call;
  77.     }
  78.     /**
  79.      * Collects data for the given Request and Response.
  80.      *
  81.      * @param Request $request A Request instance
  82.      * @param Response $response A Response instance
  83.      * @param Throwable|null $exception An Exception instance
  84.      *
  85.      * @api
  86.      */
  87.     public function collect(Request $requestResponse $responseThrowable $exception null): void
  88.     {
  89. // Nothing to do here, we implemented our own methods by hooking into the events
  90.     }
  91.     /**
  92.      * Returns the name of the collector.
  93.      * @return string The collector name
  94.      * @api
  95.      */
  96.     public function getName(): string
  97.     {
  98.         return 'phpro.soap_client';
  99.     }
  100.     /**
  101.      * Get all soap calls
  102.      * @return SoapCall[]
  103.      */
  104.     public function getCalls(): array
  105.     {
  106.         return $this->data['calls'];
  107.     }
  108.     /**
  109.      * Setter method for Stopwatch
  110.      * @param Stopwatch|null $stopwatch
  111.      * @return $this
  112.      */
  113.     public function setStopwatch(?Stopwatch $stopwatch null): self
  114.     {
  115.         $this->stopwatch $stopwatch;
  116.         return $this;
  117.     }
  118.     /**
  119.      * Get the total timing of all soap calls
  120.      * @return int
  121.      */
  122.     public function getTotalTiming(): int
  123.     {
  124.         $timing 0;
  125.         /** @var SoapCall $call */
  126.         foreach ($this->data['calls'] as $call) {
  127.             $timing += $call->getTiming();
  128.         }
  129.         return $timing;
  130.     }
  131.     /**
  132.      * Gets the average timing of all soap calls
  133.      * @return float
  134.      */
  135.     public function getAverageTiming(): float
  136.     {
  137.         $totalTiming $this->getTotalTiming();
  138.         $totalCalls = (float) count($this->data['calls']);
  139.         return $totalTiming / ($totalCalls <= $totalCalls);
  140.     }
  141.     public function getLowestTiming(): int
  142.     {
  143.         $lowestTiming null;
  144.         /**
  145.          * @var SoapCall $call
  146.          */
  147.         foreach($this->data['calls'] as $call){
  148.             if ($lowestTiming === null){
  149.                 $lowestTiming $call->getTiming();
  150.             }
  151.             elseif ($call->getTiming() < $lowestTiming){
  152.                 $lowestTiming $call->getTiming();
  153.             }
  154.         }
  155.         return $lowestTiming;
  156.     }
  157.     public function getHighestTiming(): int
  158.     {
  159.         $highestTiming null;
  160.         /**
  161.          * @var SoapCall $call
  162.          */
  163.         foreach($this->data['calls'] as $call){
  164.             if ($highestTiming === null){
  165.                 $highestTiming $call->getTiming();
  166.             }
  167.             elseif ($call->getTiming() > $highestTiming){
  168.                 $highestTiming $call->getTiming();
  169.             }
  170.         }
  171.         return $highestTiming;
  172.     }
  173.     public function getTotalResponseBodySize(): float
  174.     {
  175.         $size 0.0;
  176.         /**
  177.          * @var SoapCall $call
  178.          */
  179.         foreach($this->data['calls'] as $call){
  180.             $size += (float) strlen($call->getResponseBody());
  181.         }
  182.         return $size 1024;
  183.     }
  184.     public function getTotalReceivedSize(): float
  185.     {
  186.         $size 0.0;
  187.         /**
  188.          * @var SoapCall $call
  189.          */
  190.         foreach($this->data['calls'] as $call){
  191.             $size += (float) strlen($call->getResponseBody());
  192.             $size += (float) strlen($call->getResponseHeaders());
  193.         }
  194.         return $size 1024;
  195.     }
  196.     public function getTotalRequestBodySize(): float
  197.     {
  198.         $size 0.0;
  199.         /**
  200.          * @var SoapCall $call
  201.          */
  202.         foreach($this->data['calls'] as $call){
  203.             $size += (float) strlen($call->getRequestBody());
  204.         }
  205.         return $size 1024;
  206.     }
  207.     public function getTotalSentSize(): float
  208.     {
  209.         $size 0.0;
  210.         /**
  211.          * @var SoapCall $call
  212.          */
  213.         foreach($this->data['calls'] as $call){
  214.             $size += (float) strlen($call->getRequestBody());
  215.             $size += (float) strlen($call->getRequestHeaders());
  216.         }
  217.         return $size 1024;
  218.     }
  219.     public function getDataExchangeInfoPerCall(): array
  220.     {
  221.         $data = [];
  222.         /**
  223.          * @var SoapCall $call
  224.          */
  225.         foreach($this->data['calls'] as $call){
  226.             $name $call->getClientName();
  227.             $requestBodySize = (float) strlen($call->getRequestBody());
  228.             $totalRequestSize $requestBodySize + (float) strlen($call->getRequestHeaders());
  229.             $responseBodySize = (float) strlen($call->getResponseBody());
  230.             $totalResponseSize $responseBodySize + (float) strlen($call->getResponseHeaders());
  231.             $data[] = [
  232.                 'name' => $name,
  233.                 'method' => $call->getMethod(),
  234.                 'timing' => $call->getTiming(),
  235.                 'requestBodySize' => $requestBodySize 1024,
  236.                 'totalRequestSize' => $totalRequestSize 1024,
  237.                 'responseBodySize' => $responseBodySize 1024,
  238.                 'totalResponseSize' => $totalResponseSize 1024,
  239.             ];
  240.         }
  241.         return $data;
  242.     }
  243.     public function reset(): void
  244.     {
  245.         // TODO: Implement reset() method.
  246.     }
  247.     /**
  248.      * @param string $factoryClass
  249.      * @param TraceableTransport $transport
  250.      * @return $this
  251.      */
  252.     public function setTransport(string $factoryClassTraceableTransport $transport): SoapCallCollectorSubscriber
  253.     {
  254.         $this->transport->setTransport($this->transport->getClient($factoryClass), $transport);
  255.         return $this;
  256.     }
  257. }