<?php
namespace App\InsuranceCompany\Bulins\SOAP\Subscriber;
use App\SoapClient\Event\ResponseEvent as CustomResponseEvent;
use Soap\Engine\HttpBinding\SoapResponse;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CredentialMaskingSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly bool $isDebug,
)
{
}
/**
* @param CustomResponseEvent $event
*/
public function onEngineResponse(CustomResponseEvent $event)
{
if ($this->isDebug) {
return;
}
// Bulins send back the plaintext credentials if they are incorrect. Hide them in prod exceptions.
$response = $event->getResponse();
$payload = $response->getPayload();
if (str_contains($payload, 'System.Security.SecurityException: Невалидни име/парола: ')) {
$newPayload = preg_replace_callback(
'%Невалидни име/парола(.*)\n%',
static fn(array $matches): string => str_replace($matches[1], '', $matches[0]),
$payload,
);
$newResponse = new SoapResponse($newPayload);
$event->setResponse($newResponse);
}
}
/**
* @return string[]
* @noinspection PhpArrayShapeAttributeCanBeAddedInspection
*/
public static function getSubscribedEvents(): array
{
return [
CustomResponseEvent::class => 'onEngineResponse',
];
}
}