| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Services\Payment; |
| 4 |
|
| 5 |
use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 6 |
use IvyForms\Services\Translations\BackendStrings; |
| 7 |
|
| 8 |
/** |
| 9 |
* Registry of payment gateways available during form submission. |
| 10 |
*/ |
| 11 |
class PaymentGatewayRegistry |
| 12 |
{ |
| 13 |
/** |
| 14 |
* @var array<string, PaymentGatewayInterface> |
| 15 |
*/ |
| 16 |
private array $gateways = []; |
| 17 |
|
| 18 |
public function register(PaymentGatewayInterface $gateway): void |
| 19 |
{ |
| 20 |
$this->gateways[$gateway->getId()] = $gateway; |
| 21 |
} |
| 22 |
|
| 23 |
public function has(string $id): bool |
| 24 |
{ |
| 25 |
return isset($this->gateways[$id]); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* @throws InvalidArgumentException |
| 30 |
*/ |
| 31 |
public function get(string $id): PaymentGatewayInterface |
| 32 |
{ |
| 33 |
if (!isset($this->gateways[$id])) { |
| 34 |
throw new InvalidArgumentException( |
| 35 |
BackendStrings::getPaymentStrings()['payment_gateway_unavailable'] |
| 36 |
); |
| 37 |
} |
| 38 |
|
| 39 |
return $this->gateways[$id]; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* @return array<string, PaymentGatewayInterface> |
| 44 |
*/ |
| 45 |
public function all(): array |
| 46 |
{ |
| 47 |
return $this->gateways; |
| 48 |
} |
| 49 |
} |
| 50 |
|