Completed
Push — master ( 68dc85...9b7a0c )
by Ibrahim
02:37
created

Paystack::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 1
crap 3
1
<?php
2
3
namespace Yabacon;
4
5
use \Yabacon\Paystack\Helpers\Router;
6
7
class Paystack
8
{
9
10
    public $secret_key;
11
    private $routes;
12
    public $use_guzzle = false;
13
14 4
    public function __construct($secret_key)
15
    {
16 4
        if (!is_string($secret_key) || !(substr($secret_key, 0, 3)==='sk_')) {
17
            // Should never get here
18 1
            throw new \InvalidArgumentException('A Valid Paystack Secret Key must start with \'sk_\'.');
19
        }
20 3
         $this->secret_key = $secret_key;
21 3
         $this->routes = $this->definedRoutes();
22 3
    }
23
24
    public function useGuzzle()
25
    {
26
         $this->use_guzzle = true;
27
    }
28
29 3
    private function definedRoutes()
30
    {
31 3
        $routes = [];
32 3
        $files = scandir(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Paystack'.DIRECTORY_SEPARATOR.'Routes');
33 3
        foreach ($files as $file) {
34 3
            if ('php'===pathinfo($file, PATHINFO_EXTENSION)) {
35 3
                $routes[] = strtolower(substr($file, 0, strrpos($file, ".")));
36 3
            }
37 3
        }
38 3
        return $routes;
39
    }
40
41
    /**
42
     * __call
43
     * Magic Method for fetch on routes
44
     *
45
     * @param $method - a string whose title case is a class in the
46
     *                  Yabacon\Paystack\Routes namespace implementing
47
     *                  Yabacon\Paystack\Contracts\RouteInterface
48
     * @param $args - arguments sent to the magic method
49
     *
50
     * @return the result of calling /{route}/get on the api
51
     *
52
     * @access public
53
     * @see    Yabacon\Paystack\Routes\Router
54
     * @since  1.0
55
     */
56
    public function __call($method, $args)
57
    {
58
        /*
59
        attempt to call fetch when the route is called directly
60
        translates to /{root}/{get}/{id}
61
        */
62
        if (in_array($method, $this->routes, true) && count($args) === 1) {
63
            $route = new Router($method, $this);
64
            // no params, just one arg... the id
65
            $args = [[], [ Router::ID_KEY => $args[0] ] ];
66
            return $route->__call('fetch', $args);
67
        }
68
69
        // Not found is it plural?
70
        $is_plural = strripos($method, 's')===(strlen($method)-1);
71
        $singular_form = substr($method, 0, strlen($method)-1);
72
73
        if ($is_plural && in_array($singular_form, $this->routes, true)) {
74
            $route = new Router($singular_form, $this);
75
            if ((count($args) === 1 && is_array($args[0]))||(count($args) === 0)) {
76
                return $route->__call('getList', $args);
77
            }
78
        }
79
80
        // Should never get here
81
        throw new \InvalidArgumentException(
82
            'Route "' . $method . '" can only accept '.
83
            ($is_plural ?
84
                        'an optional array of paging arguments (perPage, page)'
85
                        : 'an id or code') . '.'
86
        );
87
    }
88
89
    /**
90
     * @deprecated
91
     */
92
    public static function registerAutoloader()
93
    {
94
        trigger_error('Include "src/autoload.php" instead', E_USER_NOTICE);
95
        spl_autoload_register(
96 View Code Duplication
            function ($class_name) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
                $file = dirname(__FILE__) . DIRECTORY_SEPARATOR;
98
                $file .= str_replace([ 'Yabacon\\', '\\' ], ['', DIRECTORY_SEPARATOR ], $class_name) . '.php';
99
                include_once $file;
100
            }
101
        );
102
    }
103
104
    /**
105
     * __get
106
     * Magic method to create a new Router
107
     * which performs all actions required
108
     */
109
    public function __get($name)
110
    {
111
        if (in_array($name, $this->routes, true)) {
112
            return new Router($name, $this);
113
        }
114
    }
115
}
116