src/InsuranceCompany/Bulins/SOAP/Subscriber/ErrorHandlingSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\InsuranceCompany\Bulins\SOAP\Subscriber;
  3. use App\InsuranceCompany\Bulins\Exception\Exception;
  4. use App\InsuranceCompany\Bulins\SOAP\Generated\Type\ErrorDescription;
  5. use App\InsuranceCompany\Bulins\SOAP\Generated\Type\Result;
  6. use App\InsuranceCompany\Common\Exception\NullValueException;
  7. use App\InsuranceCompany\Common\Type\CustomResultInterface;
  8. use App\InsuranceCompany\Common\Type\ResultProviderInterface;
  9. use Phpro\SoapClient\Event\ResponseEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use UnexpectedValueException;
  12. /**
  13.  * Class ErrorHandlingSubscriber
  14.  */
  15. class ErrorHandlingSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @param ResponseEvent $event
  19.      * @throws Exception
  20.      * @throws NullValueException
  21.      */
  22.     public function onClientResponse(ResponseEvent $event)
  23.     {
  24.         $response $event->getResponse();
  25.         if (!($response instanceof ResultProviderInterface)) {
  26.             throw new UnexpectedValueException('Response type is not a result provider');
  27.         }
  28.         $result $response->getResult();
  29.         if ($result === null) {
  30.             throw new NullValueException();
  31.         }
  32.         if (!($result instanceof Result)) {
  33.             if ($result instanceof CustomResultInterface) {
  34.                 return;
  35.             }
  36.             throw new UnexpectedValueException('Result is not a subclass of Result');
  37.         }
  38.         $status $result->getStatus();
  39.         switch ($status) {
  40.             case 'Error':
  41.                 $errors = [];
  42.                 foreach ($result->getErrors() as $errorDesc) {
  43.                     /**
  44.                      * @var ErrorDescription $errorDesc
  45.                      */
  46.                     $name $errorDesc->getPropertyName();
  47.                     if (strlen($name) > 0) {
  48.                         $errors[] = $name ': ' $errorDesc->getMessage();
  49.                     } else {
  50.                         $errors[] = $errorDesc->getMessage();
  51.                     }
  52.                 }
  53.                 throw new Exception(
  54.                     messageimplode(PHP_EOL$errors),
  55.                     errors$result->getErrors(),
  56.                 );
  57.             case 'Success':
  58.                 // nothing to do
  59.                 break;
  60.             case 'None':
  61.                 throw new UnexpectedValueException('Service returned status None');
  62.             default:
  63.                 throw new UnexpectedValueException('Undefined status: ' $status);
  64.         }
  65.     }
  66.     /**
  67.      * {@inheritdoc}
  68.      * @noinspection PhpArrayShapeAttributeCanBeAddedInspection
  69.      */
  70.     public static function getSubscribedEvents(): array
  71.     {
  72.         return [
  73.             ResponseEvent::class => 'onClientResponse',
  74.         ];
  75.     }
  76. }