|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yabacon\Paystack\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use \Closure; |
|
6
|
|
|
use \Yabacon\Paystack\Contracts\RouteInterface; |
|
7
|
|
|
use \Yabacon\Paystack\Exception\ValidationException; |
|
8
|
|
|
|
|
9
|
|
|
class Router |
|
10
|
|
|
{ |
|
11
|
|
|
private $route; |
|
12
|
|
|
private $route_class; |
|
13
|
|
|
private $methods; |
|
14
|
|
|
public static $ROUTES = [ |
|
15
|
|
|
'customer', 'page', 'plan', 'subscription', 'transaction', 'subaccount', |
|
16
|
|
|
'balance', 'bank', 'decision', 'integration', 'settlement', |
|
17
|
|
|
'transfer', 'transferrecipient', 'invoice' |
|
18
|
|
|
]; |
|
19
|
|
|
public static $ROUTE_SINGULAR_LOOKUP = [ |
|
20
|
|
|
'customers'=>'customer', |
|
21
|
|
|
'invoices'=>'invoice', |
|
22
|
|
|
'pages'=>'page', |
|
23
|
|
|
'plans'=>'plan', |
|
24
|
|
|
'subscriptions'=>'subscription', |
|
25
|
|
|
'transactions'=>'transaction', |
|
26
|
|
|
'banks'=>'bank', |
|
27
|
|
|
'settlements'=>'settlement', |
|
28
|
|
|
'transfers'=>'transfer', |
|
29
|
|
|
'transferrecipients'=>'transferrecipient', |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
const ID_KEY = 'id'; |
|
33
|
|
|
const PAYSTACK_API_ROOT = 'https://api.paystack.co'; |
|
34
|
|
|
|
|
35
|
|
|
public function __call($methd, $sentargs) |
|
36
|
|
|
{ |
|
37
|
|
|
$method = ($methd === 'list' ? 'getList' : $methd ); |
|
38
|
|
|
if (array_key_exists($method, $this->methods) && is_callable($this->methods[$method])) { |
|
39
|
|
|
return call_user_func_array($this->methods[$method], $sentargs); |
|
40
|
|
|
} else { |
|
41
|
|
|
throw new \Exception('Function "' . $method . '" does not exist for "' . $this->route . '".'); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
5 |
|
public static function singularFor($method) |
|
46
|
|
|
{ |
|
47
|
|
|
return ( |
|
48
|
5 |
|
array_key_exists($method, Router::$ROUTE_SINGULAR_LOOKUP) ? |
|
49
|
5 |
|
Router::$ROUTE_SINGULAR_LOOKUP[$method] : |
|
50
|
|
|
null |
|
51
|
5 |
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
5 |
|
public function __construct($route, $paystackObj) |
|
55
|
|
|
{ |
|
56
|
5 |
|
$routes = $this->getAllRoutes($paystackObj); |
|
57
|
|
|
|
|
58
|
5 |
|
if (!in_array($route, $routes)) { |
|
59
|
3 |
|
throw new ValidationException( |
|
60
|
3 |
|
"Route '{$route}' does not exist." |
|
61
|
3 |
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
3 |
|
$this->route = strtolower($route); |
|
65
|
3 |
|
$this->route_class = $this->getRouteClass($paystackObj); |
|
66
|
|
|
|
|
67
|
3 |
|
$mets = get_class_methods($this->route_class); |
|
68
|
3 |
|
if (empty($mets)) { |
|
69
|
|
|
throw new \InvalidArgumentException('Class "' . $this->route . '" does not exist.'); |
|
70
|
|
|
} |
|
71
|
|
|
// add methods to this object per method, except root |
|
72
|
3 |
|
foreach ($mets as $mtd) { |
|
73
|
3 |
|
if ($mtd === 'root') { |
|
74
|
3 |
|
continue; |
|
75
|
|
|
} |
|
76
|
3 |
|
$mtdFunc = function ( |
|
77
|
|
|
array $params = [ ], |
|
78
|
|
|
array $sentargs = [ ] |
|
79
|
|
|
) use ( |
|
80
|
|
|
$mtd, |
|
81
|
|
|
$paystackObj |
|
82
|
|
|
) { |
|
83
|
|
|
$interface = call_user_func($this->route_class . '::' . $mtd); |
|
84
|
|
|
// TODO: validate params and sentargs against definitions |
|
85
|
|
|
$caller = new Caller($paystackObj); |
|
86
|
|
|
return $caller->callEndpoint($interface, $params, $sentargs); |
|
87
|
3 |
|
}; |
|
88
|
3 |
|
$this->methods[$mtd] = \Closure::bind($mtdFunc, $this, get_class()); |
|
89
|
3 |
|
} |
|
90
|
3 |
|
} |
|
91
|
|
|
|
|
92
|
5 |
|
private function getAllRoutes($paystackObj) |
|
93
|
|
|
{ |
|
94
|
5 |
|
return array_merge(static::$ROUTES, array_keys($paystackObj->custom_routes)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
3 |
|
private function getRouteClass($paystackObj) |
|
98
|
|
|
{ |
|
99
|
|
|
try { |
|
100
|
3 |
|
return $paystackObj->custom_routes[$this->route]; |
|
101
|
2 |
|
} catch (\Exception $execption) { |
|
102
|
2 |
|
return 'Yabacon\\Paystack\\Routes\\' . ucwords($this->route); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|