Completed
Push — master ( 6ce54b...07dfb0 )
by Ibrahim
02:54
created

Router::__construct()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5.583

Importance

Changes 0
Metric Value
cc 5
eloc 21
nc 5
nop 2
dl 0
loc 33
ccs 15
cts 21
cp 0.7143
crap 5.583
rs 8.439
c 0
b 0
f 0
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 3
    public function __construct($route, $paystackObj)
55
    {
56 3
        if (!in_array($route, Router::$ROUTES)) {
57 3
            throw new ValidationException(
58 3
                "Route '{$route}' does not exist."
59 3
            );
60
        }
61
62 1
        $this->route = strtolower($route);
63 1
        $this->route_class = 'Yabacon\\Paystack\\Routes\\' . ucwords($route);
64
65 1
        $mets = get_class_methods($this->route_class);
66 1
        if (empty($mets)) {
67
            throw new \InvalidArgumentException('Class "' . $this->route . '" does not exist.');
68
        }
69
        // add methods to this object per method, except root
70 1
        foreach ($mets as $mtd) {
71 1
            if ($mtd === 'root') {
72 1
                continue;
73
            }
74 1
            $mtdFunc = function (
75
                array $params = [ ],
76
                array $sentargs = [ ]
77
            ) use (
78
                $mtd,
79
                $paystackObj
80
            ) {
81
                $interface = call_user_func($this->route_class . '::' . $mtd);
82
                // TODO: validate params and sentargs against definitions
83
                $caller = new Caller($paystackObj);
84
                return $caller->callEndpoint($interface, $params, $sentargs);
85 1
            };
86 1
            $this->methods[$mtd] = \Closure::bind($mtdFunc, $this, get_class());
87 1
        }
88 1
    }
89
}
90