Completed
Push — master ( 890e48...201e88 )
by Ibrahim
02:37
created

Paystack::handleSingular()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 7
cp 0.7143
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
crap 2.0932
1
<?php
2
3
namespace Yabacon;
4
5
use \Yabacon\Paystack\Helpers\Router;
6
use \Yabacon\Paystack\Exception\ValidationException;
7
8
class Paystack
9
{
10
    public $secret_key;
11
    public $use_guzzle = false;
12
13 9
    public function __construct($secret_key)
14
    {
15 9
        if (!is_string($secret_key) || !(substr($secret_key, 0, 3)==='sk_')) {
16 1
            throw new \InvalidArgumentException('A Valid Paystack Secret Key must start with \'sk_\'.');
17
        }
18 8
        $this->secret_key = $secret_key;
19 8
    }
20
21 1
    public function useGuzzle()
22
    {
23 1
        $this->use_guzzle = true;
24 1
    }
25
26 1
    public function __call($method, $args)
27
    {
28 1
        if ($singular_form = Router::singularFor($method)) {
29
            $this->handlePlural($singular_form, $method, $args);
30
        }
31 1
        $this->handleSingular($method, $args);
32
    }
33
34
    private function handlePlural($singular_form, $method, $args)
35
    {
36
        if ((count($args) === 1 && is_array($args[0]))||(count($args) === 0)) {
37
            return $this->{$singular_form}->__call('getList', $args);
38
        }
39
        throw new \InvalidArgumentException(
40
            'Route "' . $method . '" can only accept an optional array of filters and '
41
            .'paging arguments (perPage, page).'
42
        );
43
    }
44
45 1
    private function handleSingular($method, $args)
46
    {
47 1
        if (count($args) === 1) {
48
            $args = [[], [ Router::ID_KEY => $args[0] ] ];
49
            return $this->{$method}->__call('fetch', $args);
50
        }
51 1
        throw new \InvalidArgumentException(
52 1
            'Route "' . $method . '" can only accept an id or code.'
53 1
        );
54
    }
55
56
    /**
57
     * @deprecated
58
     */
59
    public static function registerAutoloader()
60
    {
61
        trigger_error('Include "src/autoload.php" instead', E_USER_NOTICE);
62
        spl_autoload_register(
63 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...
64
                $file = dirname(__FILE__) . DIRECTORY_SEPARATOR;
65
                $file .= str_replace([ 'Yabacon\\', '\\' ], ['', DIRECTORY_SEPARATOR ], $class_name) . '.php';
66
                include_once $file;
67
            }
68
        );
69
    }
70
71 1
    public function __get($name)
72
    {
73 1
        return new Router($name, $this);
74
    }
75
}
76