Completed
Push — master ( e813e3...7dc83c )
by Johnny
02:20
created

ResourceAbstract   B

Complexity

Total Complexity 42

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 42
c 3
b 1
f 0
lcom 1
cbo 5
dl 0
loc 144
ccs 0
cts 87
cp 0
rs 8.2951

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
D validate_arguments() 0 43 25
C call() 0 51 12
A getClient() 0 3 1
A getResourceName() 0 3 1
A getMethods() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like ResourceAbstract often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ResourceAbstract, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Redbox\Twitch\Resource;
3
use Redbox\Twitch\Exception;
4
use Redbox\Twitch\Client;
5
use Redbox\Twitch\Transport\HttpRequest;
6
use Redbox\Twitch;
7
8
class ResourceAbstract
9
{
10
    /**
11
     * @var array
12
     */
13
    private $methods;
14
15
    /**
16
     * @var Client
17
     */
18
    private $client;
19
20
    /**
21
     * @var string
22
     */
23
    private $resource_name;
24
25
    /**
26
     * @var array
27
     */
28
    private $url_parts = array();
29
30
31
32
    public function __construct(Client $client = null, $resource_name = "", $declaration = [])
33
    {
34
        $this->client        = $client;
35
        $this->resource_name = $resource_name;
36
        $this->methods       = isset($declaration['methods']) ? $declaration['methods'] : [];
37
38
        // If this line gives errros .. Comment it out because somewhere in the resource it self is an error.
39
    //    $client->registerResource($this->resource_name, $this);
40
        // Todo validate resources
41
    }
42
43
    private function validate_arguments($method_name = '', $args = array())
44
    {
45
        if (isset($this->methods[$method_name]) === true) {
46
            $method = $this->methods[$method_name];
47
            if (isset($method['parameters']) === true and is_array($method['parameters']) == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
48
                $parameters = $method['parameters'];
49
                foreach ($parameters as $name => $options) {
50
                    switch ($options['type']) {
51
                        case 'integer':
52
                            // todo implment default value as seen in team
53
54
                            // Required
55
                            if (isset($options['required']) === true and $options['required'] === true and isset($args[$name]) == false)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
56
                                throw new Exception\RuntimeException($this->resource_name . ' requires parameter ' . $name . ' to be given for method ' . $method);
57
58
                            // Min
59
                            if (isset($options['min']) === true and (isset($args[$name]) === true and $args[$name] < $options['min']))
60
                                throw new Exception\RuntimeException($this->resource_name . ' requires parameter ' . $name . ' to have a minimum value of ' . $options['min'] . ' for method ' . $method);
61
62
                            // Min
63
                            if (isset($options['max']) === true and (isset($args[$name]) === true and $args[$name] > $options['max']))
64
                                throw new Exception\RuntimeException($this->resource_name . ' requires parameter ' . $name . ' to have a maximum value of ' . $options['min'] . ' for method ' . $method);
65
66
                            break;
67
                        case 'string':
68
69
                            // Required
70
                            if (isset($options['required']) === true and $options['required'] === true and isset($args[$name]) === false)
71
                                throw new Exception\RuntimeException($this->resource_name . ' requires parameter ' . $name . ' to be given for method ' . $method);
72
73
                            // Url Part
74
                            if (isset($options['url_part']) === true and $options['url_part'] === true and isset($args[$name]) === false) {
75
                                throw new Exception\RuntimeException($this->resource_name . ' requires parameter ' . $name . ' to be given for method ' . $method);
76
                            } else if (isset($options['url_part']) === true and $options['url_part'] == true and isset($args[$name]) === true) {
77
                                $this->url_parts[$method_name][':' . $name] = array('name' => $name, 'value' => $args[$name]);
78
                            }
79
                            break;
80
                    }
81
                }
82
            }
83
        }
84
        return true;
85
    }
86
87
    public function call($method, $arguments = array(), $body = array())
88
    {
89
        if ($this->validate_arguments($method, $arguments) == true)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
90
        {
91
92
            $headers = array();
93
            $headers['Accept']    = 'application/vnd.twitchtv.v3+json';
94
            $headers['Client-ID'] = $this->client->getClientId();
95
96
            if (isset($this->methods[$method]['requiresAuth']) == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
97
                if ($this->methods[$method]['requiresAuth'] == true && !$this->client->getAccessToken()) {
98
                    throw new Exception\AuthorizationRequiredException('Method: '.$method.' requires authorization. Did you forget to use setAccessToken() ?');
99
                }
100
            }
101
102
            if ($this->client->getAccessToken())
103
                $headers['Authorization'] = 'OAuth ' . $this->client->getAccessToken();
104
105
106
            if (isset($this->url_parts[$method]) == true and is_array($this->url_parts[$method]) == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
107
                if (count($this->url_parts[$method]) > 0) {
108
                    foreach ($this->url_parts[$method] as $key => $url_part) {
109
                        $this->methods[$method]['path'] = str_replace($key, $url_part['value'], $this->methods[$method]['path']);
110
                        unset($arguments[$url_part['name']]);
111
                    }
112
                }
113
            }
114
115
            $url = '/'.$this->methods[$method]['path'];
116
117
            $count = 0;
118
            while ($value = current($arguments)) {
119
                $url .= (($count > 0) ? '&' : '?').key($arguments).'='.$value;
120
                next($arguments);
121
                $count++;
122
            }
123
124
            $request = new HttpRequest(
125
                $url,
126
                $this->methods[$method]['httpMethod'],
127
                $headers,
128
                $body
129
            );
130
131
            $response = $this->client->getTransport()->sendRequest(
132
                $request
133
            );
134
135
            return $response;
136
        }
137
    }
138
139
    public function getClient() {
140
        return $this->client;
141
    }
142
143
    public function getResourceName() {
144
        return $this->resource_name;
145
    }
146
147
    public function getMethods() {
148
        return $this->methods;
149
    }
150
151
}