Passed
Push — master ( 937194...4bd598 )
by Jared
02:18
created

JCRequest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 94
Duplicated Lines 29.79 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 97.73%

Importance

Changes 0
Metric Value
dl 28
loc 94
c 0
b 0
f 0
wmc 13
lcom 1
cbo 4
ccs 43
cts 44
cp 0.9773
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A request() 0 4 1
A get() 0 6 2
A post() 7 7 1
A put() 7 7 1
A patch() 7 7 1
A delete() 7 7 1
A head() 0 6 1
A manipulateUrl() 0 8 1
A manipulateParams() 0 20 4

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
 * Created by PhpStorm.
4
 * User: jaredchu
5
 * Date: 31/05/2017
6
 * Time: 09:40
7
 */
8
9
namespace JC;
10
11
12
use GuzzleHttp\Client;
13
use Purl\Url;
14
use JC\Enums\Method;
15
16
class JCRequest implements iJCRequest
17
{
18 7
    public static function request($method, $url, $guzzleOptions)
19
    {
20 7
        return new JCResponse((new Client())->request($method, $url, static::manipulateParams($guzzleOptions)));
21
    }
22
23 1
    public static function get($url, $params = null, $headers = [], $options = [])
24
    {
25 1
        return static::request(Method::GET, is_array($params) ? static::manipulateUrl($url, $params) : $url, [
26
            'headers' => $headers
27 1
        ]);
28
    }
29
30 2 View Code Duplication
    public static function post($url, $params = null, $headers = [], $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
31
    {
32 2
        return static::request(Method::POST, $url, [
33 2
            'headers' => $headers,
34
            'params' => $params
35 2
        ]);
36
    }
37
38 1 View Code Duplication
    public static function put($url, $params = null, $headers = [], $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
39
    {
40 1
        return static::request(Method::PUT, $url, [
41 1
            'headers' => $headers,
42
            'params' => $params
43 1
        ]);
44
    }
45
46 1 View Code Duplication
    public static function patch($url, $params = null, $headers = [], $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
47
    {
48 1
        return static::request(Method::PATCH, $url, [
49 1
            'headers' => $headers,
50
            'params' => $params
51 1
        ]);
52
    }
53
54 1 View Code Duplication
    public static function delete($url, $params = null, $headers = [], $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
55
    {
56 1
        return static::request(Method::DELETE, $url, [
57 1
            'headers' => $headers,
58
            'params' => $params
59 1
        ]);
60
    }
61
62 2
    public static function head($url, $headers = [], $options = [])
63
    {
64 1
        return static::request(Method::HEAD, $url, [
65 2
            'headers' => $headers,
66 1
        ]);
67
    }
68
69
    /**
70
     * Manipulate the url & params for GET request
71
     *
72
     * @param string $url
73
     * @param array $params
74
     * @return string
75
     */
76 1
    protected static function manipulateUrl($url, $params = [])
77
    {
78 1
        $urlObject = Url::parse($url);
79 1
        $queryData = array_merge($urlObject->query->getData(), $params);
80 1
        $urlObject->query->setData($queryData);
81
82 1
        return $urlObject->getUrl();
83
    }
84
85
    /**
86
     * @param array $guzzleOptions
87
     * @return array
88
     */
89 7
    protected static function manipulateParams($guzzleOptions)
90
    {
91 7
        if (isset($guzzleOptions['params'])) {
92 5
            $params = $guzzleOptions['params'];
93 5
            unset($guzzleOptions['params']);
94
95 5
            if (is_array($params)) {
96 4
                $guzzleOptions['form_params'] = $params;
97 4
            } else {
98 1
                $jsonObject = json_decode($params);
99 1
                if (json_last_error() == JSON_ERROR_NONE) {
100 1
                    $guzzleOptions['json'] = $jsonObject;
101 1
                } else {
102
                    $guzzleOptions['body'] = $params;
103
                }
104
            }
105 5
        }
106
107 7
        return $guzzleOptions;
108
    }
109
}