src/InsuranceCompany/Bulins/SOAP/Subscriber/CredentialMaskingSubscriber.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\InsuranceCompany\Bulins\SOAP\Subscriber;
  3. use App\SoapClient\Event\ResponseEvent as CustomResponseEvent;
  4. use Soap\Engine\HttpBinding\SoapResponse;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. class CredentialMaskingSubscriber implements EventSubscriberInterface
  7. {
  8.     public function __construct(
  9.         private readonly bool $isDebug,
  10.     )
  11.     {
  12.     }
  13.     /**
  14.      * @param CustomResponseEvent $event
  15.      */
  16.     public function onEngineResponse(CustomResponseEvent $event)
  17.     {
  18.         if ($this->isDebug) {
  19.             return;
  20.         }
  21.         // Bulins send back the plaintext credentials if they are incorrect. Hide them in prod exceptions.
  22.         $response $event->getResponse();
  23.         $payload $response->getPayload();
  24.         if (str_contains($payload'System.Security.SecurityException: Невалидни име/парола: ')) {
  25.             $newPayload preg_replace_callback(
  26.                 '%Невалидни име/парола(.*)\n%',
  27.                 static fn(array $matches): string => str_replace($matches[1], ''$matches[0]),
  28.                 $payload,
  29.             );
  30.             $newResponse = new SoapResponse($newPayload);
  31.             $event->setResponse($newResponse);
  32.         }
  33.     }
  34.     /**
  35.      * @return string[]
  36.      * @noinspection PhpArrayShapeAttributeCanBeAddedInspection
  37.      */
  38.     public static function getSubscribedEvents(): array
  39.     {
  40.         return [
  41.             CustomResponseEvent::class => 'onEngineResponse',
  42.         ];
  43.     }
  44. }