Configs
1 year ago
Paypal
1 year ago
GatewayBase.php
1 year ago
GatewayFactory.php
1 year ago
PaypalGateway.php
1 year ago
GatewayFactory.php
41 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Payment gateway factory class |
| 4 | * |
| 5 | * @package Tutor\Ecommerce |
| 6 | * @author Themeum |
| 7 | * @link https://themeum.com |
| 8 | * @since 3.0.0 |
| 9 | */ |
| 10 | |
| 11 | namespace Tutor\PaymentGateways; |
| 12 | |
| 13 | /** |
| 14 | * Create object of a payment gateway |
| 15 | */ |
| 16 | abstract class GatewayFactory { |
| 17 | |
| 18 | /** |
| 19 | * Create an instance of the specified payment gateway. |
| 20 | * |
| 21 | * @param string $gateway The fully qualified class name of the gateway. |
| 22 | * @return GatewayBase |
| 23 | * @throws \InvalidArgumentException If the class does not exist or is not an instance of GatewayBase. |
| 24 | */ |
| 25 | public static function create( string $gateway ): GatewayBase { |
| 26 | if ( ! class_exists( $gateway ) ) { |
| 27 | throw new \InvalidArgumentException( "Gateway class {$gateway} does not exist." ); |
| 28 | } |
| 29 | |
| 30 | $obj = new $gateway(); |
| 31 | |
| 32 | // Ensure the object is an instance of GatewayBase. |
| 33 | if ( ! $obj instanceof GatewayBase ) { |
| 34 | throw new \InvalidArgumentException( "Gateway {$gateway} must be an instance of GatewayBase." ); |
| 35 | } |
| 36 | |
| 37 | return $obj; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 |