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

JCRequest::manipulateParams()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0058

Importance

Changes 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
ccs 13
cts 14
cp 0.9286
rs 9.2
cc 4
eloc 13
nc 4
nop 1
crap 4.0058
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
}