Completed
Push — master ( a70f1d...75b407 )
by Ibrahim
06:01
created

Paystack   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 68
Duplicated Lines 7.35 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 71.05%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 5
loc 68
ccs 27
cts 38
cp 0.7105
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 3
A useGuzzle() 0 4 1
A registerAutoloader() 5 11 1
A __get() 0 4 1
A __call() 0 7 2
A handlePlural() 0 10 4
A handleSingular() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 14
    public function __construct($secret_key)
14
    {
15 14
        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 13
        $this->secret_key = $secret_key;
19 13
    }
20
21 1
    public function useGuzzle()
22
    {
23 1
        $this->use_guzzle = true;
24 1
    }
25
26 4
    public function __call($method, $args)
27
    {
28 4
        if ($singular_form = Router::singularFor($method)) {
29 1
            return $this->handlePlural($singular_form, $method, $args);
30
        }
31 3
        return $this->handleSingular($method, $args);
32
    }
33
34 1
    private function handlePlural($singular_form, $method, $args)
35
    {
36 1
        if ((count($args) === 1 && is_array($args[0]))||(count($args) === 0)) {
37
            return $this->{$singular_form}->__call('getList', $args);
38
        }
39 1
        throw new \InvalidArgumentException(
40 1
            'Route "' . $method . '" can only accept an optional array of filters and '
41 1
            .'paging arguments (perPage, page).'
42 1
        );
43
    }
44
45 3
    private function handleSingular($method, $args)
46
    {
47 3
        if (count($args) === 1) {
48 1
            $args = [[], [ Router::ID_KEY => $args[0] ] ];
49 1
            return $this->{$method}->__call('fetch', $args);
50
        }
51 2
        throw new \InvalidArgumentException(
52 2
            'Route "' . $method . '" can only accept an id or code.'
53 2
        );
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 2
    public function __get($name)
72
    {
73 2
        return new Router($name, $this);
74
    }
75
}
76