<?php
declare(strict_types=1);
namespace Abap\PaymentBundle\Handler\NotifyRequest;
final class CompositeNotifyRequestHandler implements NotifyRequestHandlerInterface
{
private array $gatewaysConfig;
private array $handlers = [];
public function __construct(array $gatewaysConfig)
{
$this->gatewaysConfig = $gatewaysConfig;
}
public function add(NotifyRequestHandlerInterface $handler, string $serviceId): void
{
str_replace('\\', '_', $serviceId);
$this->handlers[$serviceId] = $handler;
}
public function handle(string $gatewayName, string $paymentClass = null): void
{
if (!array_key_exists($gatewayName, $this->gatewaysConfig)) {
throw new \InvalidArgumentException(sprintf('Gateway "%s" is not supported for notify request.', $gatewayName));
}
$gatewayConfig = $this->gatewaysConfig[$gatewayName];
$paymentClass = $gatewayConfig['storage'];
$handler = $this->getHandler($gatewayConfig['notify_request_handler']);
$handler->handle($gatewayName, $paymentClass);
}
private function getHandler(string $serviceId): NotifyRequestHandlerInterface
{
str_replace('\\', '_', $serviceId);
if (str_starts_with($serviceId, '@')) {
$serviceId = substr($serviceId, strlen('@'));
}
if (!array_key_exists($serviceId, $this->handlers)) {
throw new \InvalidArgumentException("Handler {$serviceId} not found");
}
return $this->handlers[$serviceId];
}
}