src/InsuranceCompany/Common/Insis/Subscriber/ErrorHandlingSubscriber.php line 142

Open in your IDE?
  1. <?php
  2. namespace App\InsuranceCompany\Common\Insis\Subscriber;
  3. use App\InsuranceCompany\Common\Insis\InsuranceSvcRsTypeInterface;
  4. use App\InsuranceCompany\Common\Insis\InsuranceSvcTypeInterface;
  5. use App\InsuranceCompany\Common\Insis\RegisterAddCovAnnexRs;
  6. use Phpro\SoapClient\Event\FaultEvent;
  7. use Phpro\SoapClient\Event\ResponseEvent;
  8. use Phpro\SoapClient\Exception\SoapException;
  9. use Phpro\SoapClient\Type\ResultInterface;
  10. use Psr\Log\LoggerInterface;
  11. use RuntimeException;
  12. use SoapFault;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Throwable;
  15. /**
  16.  * Class ErrorHandlingSubscriber
  17.  */
  18. class ErrorHandlingSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var string
  22.      */
  23.     protected string $exceptionClass;
  24.     /**
  25.      * @var string
  26.      */
  27.     protected string $insuranceSvcTypeClass;
  28.     /**
  29.      * @var string
  30.      */
  31.     protected string $insuranceSvcTypeRsClass;
  32.     /**
  33.      * Constructor
  34.      *
  35.      * @param LoggerInterface $logger
  36.      */
  37.     public function __construct(
  38.         protected LoggerInterface $logger,
  39.     )
  40.     {
  41.     }
  42.     /**
  43.      * @param ResponseEvent $event
  44.      */
  45.     public function onClientResponse(ResponseEvent $event)
  46.     {
  47.         $response $event->getResponse();
  48.         if (!($response instanceof InsuranceSvcTypeInterface)) {
  49.             throw new RuntimeException('Invalid response type received');
  50.         }
  51.         /**
  52.          * @noinspection PhpPossiblePolymorphicInvocationInspection
  53.          * @todo ErrorInterface
  54.          */
  55.         $error $response->getError();
  56.         if ($error === null) {
  57.             return;
  58.         }
  59.         $module $error->getModule();
  60.         $errorId $error->getErrorId();
  61.         $errorDescr $error->getErrorDescr();
  62. //        if ($errorDescr === 'PROCESS=FAILURE; ERROR_MESSAGE=6600_19: Стикерът съществува в Регистъра за Бланки под строга отчетност, но вече е използван или регистриран в друг офис.;') {
  63. //            $errorDescr = 'PROCESS=SUCCESSFULLY; ANNEX_ID=3; INSTALL_ID=3114602626;';
  64. //        }
  65.         if (
  66.             $module === 'WEBSVC' &&
  67.             $errorId === 'INSIS_ERROR' &&
  68.             str_contains($errorDescr'PROCESS=') &&
  69.             str_contains($errorDescr';')
  70.         ) {
  71.             $parts array_filter(array_map('trim'explode(';'$errorDescr)));
  72.             $errorInfo = [];
  73.             $errorDetails = [];
  74.             foreach ($parts as $part) {
  75.                 $parts2 array_map('trim'explode('='$part2));
  76.                 if (count($parts2) > 1) {
  77.                     $key $parts2[0];
  78.                     $value $parts2[1];
  79.                     $errorInfo[$key] = $value;
  80.                     if ($key === 'ERROR_MESSAGE') {
  81.                         $errorParts array_map('trim'explode(':'$value2));
  82.                         if (count($errorParts) > 1) {
  83.                             $errorDetails = [
  84.                                 'code' => $errorParts[0],
  85.                                 'message' => $errorParts[1],
  86.                             ];
  87.                         }
  88.                     }
  89.                 }
  90.                 else {
  91.                     $errorInfo[] = $parts2[0];
  92.                 }
  93.             }
  94.             switch ($errorInfo['PROCESS']) {
  95.                 case 'FAILURE':
  96.                     if (!empty($errorDetails)) {
  97.                         $errorDescr $errorDetails['message'] . ' (' $errorDetails['code'] . ')';
  98.                     }
  99.                     break;
  100.                 case 'SUCCESSFULLY':
  101.                     $event->registerResponse($this->getResponse(new RegisterAddCovAnnexRs(
  102.                         $errorInfo['ANNEX_ID'] ?? null,
  103.                         $errorInfo['INSTALL_ID'] ?? null,
  104.                         $errorInfo['NEW_GREEN_CARD_NO'] ?? null,
  105.                     )));
  106.                     return;
  107.                 default:
  108.                     $errorDescr 'Unknown process value: ' var_export($errorInfo['PROCESS'], true);
  109.                     break;
  110.             }
  111.         }
  112.         throw new $this->exceptionClass(
  113.             $errorDescr ' (' $error->getErrorID() . '@' $error->getModule() . ')'0null,
  114.             $errorId$errorDescr$module,
  115.         );
  116.     }
  117.     /**
  118.      * @param FaultEvent $event
  119.      */
  120.     public function onClientFault(FaultEvent $event)
  121.     {
  122.         $soapException $event->getSoapException();
  123.         if (
  124.             $soapException->getMessage() === 'JBO-26061: Error while opening JDBC connection.' || (
  125.                 str_starts_with(
  126.                     $soapException->getMessage(),
  127.                     'WEB.COMMON.LOGIN.AUTH_FAILED: weblogic.common.resourcepool.ResourceLimitException: No resources ' .
  128.                     'currently available in pool '
  129.                 ) &&
  130.                 str_ends_with(
  131.                     $soapException->getMessage(),
  132.                     ' to allocate to applications, please increase the size of the pool and retry..'
  133.                 )
  134.             )
  135.         ) {
  136.             throw $this->getInsuranceCompanyUnavailableException($soapException);
  137.         }
  138.         $soapFault $soapException->getPrevious();
  139.         if (!($soapFault instanceof SoapFault)) {
  140.             return;
  141.         }
  142.         if (str_contains(
  143.             $soapFault->detail?->exception ?? '',
  144.             'Caused by: oracle.jbo.JboException: Proxy session initialization failed!ORA-28000: The account is locked.',
  145.         )) {
  146.             throw $this->getInsuranceCompanyUnavailableException($soapException);
  147.         }
  148.     }
  149.     private function getResponse(ResultInterface $result): InsuranceSvcTypeInterface
  150.     {
  151.         /**
  152.          * @var InsuranceSvcTypeInterface $insuranceSvc
  153.          */
  154.         $insuranceSvc = new $this->insuranceSvcTypeClass();
  155.         /**
  156.          * @var InsuranceSvcRsTypeInterface $insuranceSvcRs
  157.          */
  158.         $insuranceSvcRs = new $this->insuranceSvcTypeRsClass();
  159.         // TODO
  160.         /** @noinspection PhpUndefinedFieldInspection */
  161.         $insuranceSvcRs->_result $result;
  162.         $insuranceSvc->setInsuranceSvcRs($insuranceSvcRs);
  163.         return $insuranceSvc;
  164.     }
  165.     /**
  166.      * @param Throwable $previous
  167.      * @return SoapException
  168.      */
  169.     private function getInsuranceCompanyUnavailableException(Throwable $previous): SoapException
  170.     {
  171.         return new SoapException(
  172.             'В момента няма достъп до застрахователната компания',
  173.             $previous->getCode(),
  174.             $previous,
  175.         );
  176.     }
  177.     /**
  178.      * {@inheritdoc}
  179.      * @noinspection PhpArrayShapeAttributeCanBeAddedInspection
  180.      */
  181.     public static function getSubscribedEvents(): array
  182.     {
  183.         return [
  184.             ResponseEvent::class => 'onClientResponse',
  185.             FaultEvent::class => 'onClientFault',
  186.         ];
  187.     }
  188. }