Router::__construct()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5.5069

Importance

Changes 0
Metric Value
cc 5
eloc 22
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 35
ccs 16
cts 22
cp 0.7272
crap 5.5069
rs 9.2568
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 7
    public function __construct($route, $paystackObj)
55
    {
56 7
        $routes = $this->getAllRoutes($paystackObj);
57
58 7
        if (!in_array($route, $routes)) {
59 3
            throw new ValidationException(
60 3
                "Route '{$route}' does not exist."
61 3
            );
62
        }
63
64 5
        $this->route = strtolower($route);
65 5
        $this->route_class = $this->getRouteClass($paystackObj);
66
67 5
        $mets = get_class_methods($this->route_class);
68 5
        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 5
        foreach ($mets as $mtd) {
73 5
            if ($mtd === 'root') {
74 5
                continue;
75
            }
76 5
            $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 5
            };
88 5
            $this->methods[$mtd] = \Closure::bind($mtdFunc, $this, get_class());
89 5
        }
90 5
    }
91
92 7
    private function getAllRoutes($paystackObj)
93
    {
94 7
        return array_merge(static::$ROUTES, array_keys($paystackObj->custom_routes));
95
    }
96
97 5
    private function getRouteClass($paystackObj)
98
    {
99 5
        if (isset($paystackObj->custom_routes[$this->route])) {
100 2
            return $paystackObj->custom_routes[$this->route];
101
        }
102
103 3
        return 'Yabacon\\Paystack\\Routes\\' . ucwords($this->route);
104
    }
105
}
106