src/InsuranceCompany/DallBogg/SOAP/Subscriber/ErrorHandlingSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\InsuranceCompany\DallBogg\SOAP\Subscriber;
  3. use App\InsuranceCompany\Common\Type\ResultProviderInterface;
  4. use App\InsuranceCompany\DallBogg\Exception\Exception;
  5. use App\InsuranceCompany\DallBogg\SOAP\Generated\Type\Response;
  6. use App\InsuranceCompany\DallBogg\SOAP\Generated\Type\TestServiceResponse;
  7. use Phpro\SoapClient\Event\ResponseEvent;
  8. use Psr\Log\LoggerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use UnexpectedValueException;
  11. /**
  12.  * Class DallBoggErrorHandlingSubscriber
  13.  */
  14. class ErrorHandlingSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * Constructor
  18.      *
  19.      * @param LoggerInterface $logger
  20.      */
  21.     public function __construct(
  22.         private readonly LoggerInterface $logger,
  23.     )
  24.     {
  25.     }
  26.     /**
  27.      * @param ResponseEvent $event
  28.      * @throws Exception
  29.      */
  30.     public function onClientResponse(ResponseEvent $event)
  31.     {
  32.         $response $event->getResponse();
  33.         if ($response instanceof TestServiceResponse) {
  34.             // should be fine
  35.             return;
  36.         }
  37.         if (!($response instanceof ResultProviderInterface)) {
  38.             throw new UnexpectedValueException('Response type is not a result provider');
  39.         }
  40.         $result $response->getResult();
  41.         if (!($result instanceof Response)) {
  42.             throw new UnexpectedValueException('Result type is not a Response');
  43.         }
  44.         $status $result->getStatus();
  45.         $this->logger->info('Server execution time: ' $status->getServerExecutionTime() . ' ms');
  46.         $code = (int)$status->getCode();
  47.         $message $status->getDescription();
  48.         if ($code === 0) {
  49.             return;
  50.         }
  51.         throw new Exception($result$message$code);
  52.     }
  53.     /**
  54.      * {@inheritdoc}
  55.      * @noinspection PhpArrayShapeAttributeCanBeAddedInspection
  56.      */
  57.     public static function getSubscribedEvents(): array
  58.     {
  59.         return [
  60.             ResponseEvent::class => 'onClientResponse',
  61.         ];
  62.     }
  63. }