Completed
Push — master ( 93c150...38adbe )
by Ibrahim
03:41
created

Paystack   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 80
Duplicated Lines 6.25 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 61.36%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 5
loc 80
ccs 27
cts 44
cp 0.6136
rs 10
c 0
b 0
f 0

9 Methods

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

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
    public $fallback_to_file_get_contents = true;
13
    const VERSION="2.1.15";
14
15 14
    public function __construct($secret_key)
16
    {
17 14
        if (!is_string($secret_key) || !(substr($secret_key, 0, 3)==='sk_')) {
18 1
            throw new \InvalidArgumentException('A Valid Paystack Secret Key must start with \'sk_\'.');
19
        }
20 13
        $this->secret_key = $secret_key;
21 13
    }
22
23 1
    public function useGuzzle()
24
    {
25 1
        $this->use_guzzle = true;
26 1
    }
27
28
    public static function disableFileGetContentsFallback()
29
    {
30
        Paystack::$fallback_to_file_get_contents = false;
31
    }
32
33
    public static function enableFileGetContentsFallback()
34
    {
35
        Paystack::$fallback_to_file_get_contents = true;
36
    }
37
38 4
    public function __call($method, $args)
39
    {
40 4
        if ($singular_form = Router::singularFor($method)) {
41 1
            return $this->handlePlural($singular_form, $method, $args);
42
        }
43 3
        return $this->handleSingular($method, $args);
44
    }
45
46 1
    private function handlePlural($singular_form, $method, $args)
47
    {
48 1
        if ((count($args) === 1 && is_array($args[0]))||(count($args) === 0)) {
49
            return $this->{$singular_form}->__call('getList', $args);
50
        }
51 1
        throw new \InvalidArgumentException(
52 1
            'Route "' . $method . '" can only accept an optional array of filters and '
53 1
            .'paging arguments (perPage, page).'
54 1
        );
55
    }
56
57 3
    private function handleSingular($method, $args)
58
    {
59 3
        if (count($args) === 1) {
60 1
            $args = [[], [ Router::ID_KEY => $args[0] ] ];
61 1
            return $this->{$method}->__call('fetch', $args);
62
        }
63 2
        throw new \InvalidArgumentException(
64 2
            'Route "' . $method . '" can only accept an id or code.'
65 2
        );
66
    }
67
68
    /**
69
     * @deprecated
70
     */
71
    public static function registerAutoloader()
72
    {
73
        trigger_error('Include "src/autoload.php" instead', E_USER_NOTICE);
74
        spl_autoload_register(
75 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...
76
                $file = dirname(__FILE__) . DIRECTORY_SEPARATOR;
77
                $file .= str_replace([ 'Yabacon\\', '\\' ], ['', DIRECTORY_SEPARATOR ], $class_name) . '.php';
78
                include_once $file;
79
            }
80
        );
81
    }
82
83 2
    public function __get($name)
84
    {
85 2
        return new Router($name, $this);
86
    }
87
}
88