Actions
4 years ago
Controllers
4 years ago
Exceptions
4 years ago
Helpers
4 years ago
Migrations
4 years ago
Traits
4 years ago
ValueObjects
4 years ago
BECSGateway.php
4 years ago
CheckoutGateway.php
4 years ago
CreditCardGateway.php
4 years ago
SEPAGateway.php
4 years ago
Workflow.php
4 years ago
WorkflowAction.php
4 years ago
Workflow.php
57 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\Gateways\Stripe; |
| 4 | |
| 5 | use ReflectionMethod; |
| 6 | use ReflectionParameter; |
| 7 | |
| 8 | /** |
| 9 | * Encapsulate sequential processes with a shared context. |
| 10 | */ |
| 11 | class Workflow |
| 12 | { |
| 13 | protected $container = []; |
| 14 | |
| 15 | public function __construct() |
| 16 | { |
| 17 | $this->bind( $this ); |
| 18 | } |
| 19 | |
| 20 | public function bind( $concrete ) { |
| 21 | $this->container[ get_class( $concrete ) ] = $concrete; |
| 22 | return $this; |
| 23 | } |
| 24 | |
| 25 | public function resolve( $abstract ) { |
| 26 | if( ! isset( $this->container[ $abstract ] ) ) { |
| 27 | throw new \Exception( "Abstract $abstract not found." ); |
| 28 | } |
| 29 | return $this->container[ $abstract ]; |
| 30 | } |
| 31 | |
| 32 | public function action( callable $action ) |
| 33 | { |
| 34 | if( is_a( $action, WorkflowAction::class ) ) { |
| 35 | $action->attachWorkflow( $this ); |
| 36 | } |
| 37 | |
| 38 | $reflection = new ReflectionMethod($action, '__invoke'); |
| 39 | $action( ...array_map( function( $parameter ) { |
| 40 | return $this->resolve( $this->getReflectionParameterClassName( $parameter ) ); |
| 41 | }, $reflection->getParameters() ) ); |
| 42 | |
| 43 | return $this; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @param ReflectionParameter $parameter |
| 48 | * @return string |
| 49 | */ |
| 50 | protected function getReflectionParameterClassName( ReflectionParameter $parameter ) |
| 51 | { |
| 52 | return method_exists( $parameter, 'getType' ) |
| 53 | ? $parameter->getType()->getName() |
| 54 | : $parameter->getClass()->name; |
| 55 | } |
| 56 | } |
| 57 |