Passed
Pull Request — master (#27)
by
unknown
02:48
created

Paystack::disableFileGetContentsFallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yabacon;
4
5
use \Yabacon\Paystack\Helpers\Router;
6
use Yabacon\Paystack\Contracts\RouteInterface;
7
use \Yabacon\Paystack\Exception\ValidationException;
8
9
class Paystack
10
{
11
    public $secret_key;
12
    public $use_guzzle = false;
13
    public $custom_routes = [];
14
    public static $fallback_to_file_get_contents = true;
15
    const VERSION="2.1.19";
16
17 19
    public function __construct($secret_key)
18
    {
19 19
        if (!is_string($secret_key) || !(substr($secret_key, 0, 3)==='sk_')) {
20 1
            throw new \InvalidArgumentException('A Valid Paystack Secret Key must start with \'sk_\'.');
21
        }
22 18
        $this->secret_key = $secret_key;
23 18
    }
24
25 1
    public function useGuzzle()
26
    {
27 1
        $this->use_guzzle = true;
28 1
    }
29
30 5
    public function useRoutes(array $routes)
31
    {
32 5
        foreach ($routes as $route => $class) {
33 5
            if (! is_string($route)) {
34 1
                throw new \InvalidArgumentException(
35
                    'Custom routes should map to a route class'
36 1
                );
37
            }
38
39 4
            if (! in_array(RouteInterface::class, class_implements($class))) {
40 1
                throw new \InvalidArgumentException(
41 1
                    'Custom route class ' . $class . 'should implement ' . RouteInterface::class
42 1
                );
43
            }
44 3
        }
45
        
46 3
        $this->custom_routes = $routes;
47 3
    }
48
49 1
    public static function disableFileGetContentsFallback()
50
    {
51 1
        Paystack::$fallback_to_file_get_contents = false;
52 1
    }
53
54 1
    public static function enableFileGetContentsFallback()
55
    {
56 1
        Paystack::$fallback_to_file_get_contents = true;
57 1
    }
58
59 4
    public function __call($method, $args)
60
    {
61 4
        if ($singular_form = Router::singularFor($method)) {
62 1
            return $this->handlePlural($singular_form, $method, $args);
63
        }
64 3
        return $this->handleSingular($method, $args);
65
    }
66
67 1
    private function handlePlural($singular_form, $method, $args)
68
    {
69 1
        if ((count($args) === 1 && is_array($args[0]))||(count($args) === 0)) {
70
            return $this->{$singular_form}->__call('getList', $args);
71
        }
72 1
        throw new \InvalidArgumentException(
73 1
            'Route "' . $method . '" can only accept an optional array of filters and '
74 1
            .'paging arguments (perPage, page).'
75 1
        );
76
    }
77
78 3
    private function handleSingular($method, $args)
79
    {
80 3
        if (count($args) === 1) {
81 1
            $args = [[], [ Router::ID_KEY => $args[0] ] ];
82 1
            return $this->{$method}->__call('fetch', $args);
83
        }
84 2
        throw new \InvalidArgumentException(
85 2
            'Route "' . $method . '" can only accept an id or code.'
86 2
        );
87
    }
88
89
    /**
90
     * @deprecated
91
     */
92
    public static function registerAutoloader()
93
    {
94
        trigger_error('Include "src/autoload.php" instead', E_DEPRECATED | E_USER_NOTICE);
95
        require_once(__DIR__ . '/../src/autoload.php');
96
    }
97
98 4
    public function __get($name)
99
    {
100 4
        return new Router($name, $this);
101
    }
102
}
103