src/InsuranceCompany/Uniqa/SOAP/Subscriber/ErrorHandlingSubscriber.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\InsuranceCompany\Uniqa\SOAP\Subscriber;
  3. use App\InsuranceCompany\Common\Type\ResultProviderInterface;
  4. use App\InsuranceCompany\Uniqa\Exception\Exception;
  5. use App\InsuranceCompany\Uniqa\SOAP\Generated\Type\GetPolicyDueInstallmentResult;
  6. use App\InsuranceCompany\Uniqa\SOAP\Generated\Type\Info;
  7. use App\InsuranceCompany\Uniqa\SOAP\Interfaces\ResultInterface;
  8. use Phpro\SoapClient\Event\ResponseEvent;
  9. use Psr\Log\LoggerInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use UnexpectedValueException;
  12. /**
  13.  * Class ErrorHandlingSubscriber
  14.  */
  15. class ErrorHandlingSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * Constructor
  19.      *
  20.      * @param LoggerInterface $logger
  21.      */
  22.     public function __construct(
  23.         private readonly LoggerInterface $logger,
  24.     )
  25.     {
  26.     }
  27.     /**
  28.      * @param ResponseEvent $event
  29.      * @throws Exception
  30.      */
  31.     public function onClientResponse(ResponseEvent $event)
  32.     {
  33.         $response $event->getResponse();
  34.         if (!($response instanceof ResultProviderInterface)) {
  35.             throw new UnexpectedValueException('Response type is not a result provider');
  36.         }
  37.         $result $response->getResult();
  38.         if (!($result instanceof ResultInterface)) {
  39.             throw new UnexpectedValueException('Result type is not a result');
  40.         }
  41.         $this->logger->info('Correlation ID: ' $result->getCorrelationID());
  42.         $status $result->getStatus();
  43.         $statusCode $status->getStatusType();
  44.         if ($statusCode === 0) {
  45.             return;
  46.         }
  47.         $info $status->getInfo()->getArray();
  48.         // Even if there are no unpaid installments, client name is returned and used in
  49.         // installment payment request
  50.         if ($result instanceof GetPolicyDueInstallmentResult) {
  51.             $noUnpaidInstallmentsInfo array_filter($info, static fn(Info $info) =>
  52.                 $info->getType() === 'ERROR' && $info->getCode() === 'PSM-401'
  53.             );
  54.             if (count($noUnpaidInstallmentsInfo) > 0) {
  55.                 return;
  56.             }
  57.         }
  58.         throw Exception::fromInfoAndStatus($info$statusCode);
  59.     }
  60.     /**
  61.      * {@inheritdoc}
  62.      * @noinspection PhpArrayShapeAttributeCanBeAddedInspection
  63.      */
  64.     public static function getSubscribedEvents(): array
  65.     {
  66.         return [
  67.             ResponseEvent::class => 'onClientResponse',
  68.         ];
  69.     }
  70. }