Completed
Push — master ( c1b3bb...db4538 )
by Jared
03:22
created

JCRequest::combineParams()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 8.8571
cc 5
eloc 11
nc 4
nop 1
crap 5
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jaredchu
5
 * Date: 31/05/2017
6
 * Time: 09:40
7
 */
8
9
namespace JC\HttpClient;
10
11
12
use GuzzleHttp\Client;
13
use GuzzleHttp\Exception\RequestException;
14
use JC\HttpClient\Enums\Method;
15
use Purl\Url;
16
17
class JCRequest implements JCRequestInterface
18 7
{
19
    public static function request($method, $url, $guzzleOptions)
20 7
    {
21
        try {
22
            return new JCResponse((new Client())->request($method, $url, static::combineParams($guzzleOptions)));
23 1
        } catch (RequestException $exception) {
24
            return new JCResponse($exception->getResponse());
25 1
        }
26
    }
27 1
28
    public static function get($url, $params = null, $headers = [], $options = [])
29
    {
30 2
        return static::request(Method::GET, is_array($params) ? static::combineUrl($url, $params) : $url,
31
            array_merge(['headers' => $headers], $options));
32 2
    }
33 2
34
    public static function post($url, $params = null, $headers = [], $options = [])
35 2
    {
36
        return static::request(Method::POST, $url,
37
            array_merge(['headers' => $headers, 'params' => $params], $options));
38 1
    }
39
40 1
    public static function put($url, $params = null, $headers = [], $options = [])
41 1
    {
42
        return static::request(Method::PUT, $url,
43 1
            array_merge(['headers' => $headers, 'params' => $params], $options));
44
    }
45
46 1
    public static function patch($url, $params = null, $headers = [], $options = [])
47
    {
48 1
        return static::request(Method::PATCH, $url,
49 1
            array_merge(['headers' => $headers, 'params' => $params], $options));
50
    }
51 1
52
    public static function delete($url, $params = null, $headers = [], $options = [])
53
    {
54 1
        return static::request(Method::DELETE, $url,
55
            array_merge(['headers' => $headers, 'params' => $params], $options));
56 1
    }
57 1
58
    public static function head($url, $headers = [], $options = [])
59 1
    {
60
        return static::request(Method::HEAD, $url,
61
            array_merge(['headers' => $headers], $options));
62 2
    }
63
64 1
    /**
65 2
     * Manipulate the url & params for GET request
66 1
     *
67
     * @param string $url
68
     * @param array $params
69
     * @return string
70
     */
71
    protected static function combineUrl($url, $params = [])
72
    {
73
        $urlObject = Url::parse($url);
74
        $queryData = array_merge($urlObject->query->getData(), $params);
75
        $urlObject->query->setData($queryData);
76 1
77
        return $urlObject->getUrl();
78 1
    }
79 1
80 1
    /**
81
     * @param array $guzzleOptions
82 1
     * @return array
83
     */
84
    protected static function combineParams($guzzleOptions)
85
    {
86
        if (isset($guzzleOptions['params'])) {
87
            $params = $guzzleOptions['params'];
88
            unset($guzzleOptions['params']);
89 7
90
            if (is_array($params)) {
91 7
                $guzzleOptions['form_params'] = $params;
92 5
            } elseif (is_string($params) && $jsonObject = static::jsonDecode($params)) {
0 ignored issues
show
Bug introduced by
Since jsonDecode() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of jsonDecode() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
93 5
                $guzzleOptions['json'] = $jsonObject;
94
            } else {
95 5
                $guzzleOptions['body'] = $params;
96 4
            }
97 4
        }
98 1
99 1
        return $guzzleOptions;
100 1
    }
101 1
102
    /**
103
     * @param $string
104
     * @return bool|mixed
105 5
     */
106
    private static function jsonDecode($string)
107 7
    {
108
        $jsonObject = json_decode($string);
109
        if (json_last_error() == JSON_ERROR_NONE) {
110
            return $jsonObject;
111
        } else {
112
            return false;
113
        }
114
    }
115
}