<?php
namespace App\InsuranceCompany\DallBogg\SOAP\Subscriber;
use App\InsuranceCompany\Common\Type\ResultProviderInterface;
use App\InsuranceCompany\DallBogg\Exception\Exception;
use App\InsuranceCompany\DallBogg\SOAP\Generated\Type\Response;
use App\InsuranceCompany\DallBogg\SOAP\Generated\Type\TestServiceResponse;
use Phpro\SoapClient\Event\ResponseEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use UnexpectedValueException;
/**
* Class DallBoggErrorHandlingSubscriber
*/
class ErrorHandlingSubscriber implements EventSubscriberInterface
{
/**
* Constructor
*
* @param LoggerInterface $logger
*/
public function __construct(
private readonly LoggerInterface $logger,
)
{
}
/**
* @param ResponseEvent $event
* @throws Exception
*/
public function onClientResponse(ResponseEvent $event)
{
$response = $event->getResponse();
if ($response instanceof TestServiceResponse) {
// should be fine
return;
}
if (!($response instanceof ResultProviderInterface)) {
throw new UnexpectedValueException('Response type is not a result provider');
}
$result = $response->getResult();
if (!($result instanceof Response)) {
throw new UnexpectedValueException('Result type is not a Response');
}
$status = $result->getStatus();
$this->logger->info('Server execution time: ' . $status->getServerExecutionTime() . ' ms');
$code = (int)$status->getCode();
$message = $status->getDescription();
if ($code === 0) {
return;
}
throw new Exception($result, $message, $code);
}
/**
* {@inheritdoc}
* @noinspection PhpArrayShapeAttributeCanBeAddedInspection
*/
public static function getSubscribedEvents(): array
{
return [
ResponseEvent::class => 'onClientResponse',
];
}
}