Completed
Push — master ( 8383ce...ec21f8 )
by Jared
05:41
created

JCRequest::jsonDecode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 9.6666
cc 2
eloc 6
nc 2
nop 1
crap 6
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 JC\Enums\Method;
14
use Purl\Url;
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::combineParams($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::combineUrl($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 combineUrl($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 combineParams($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
            } elseif (is_string($params) && $jsonObject = self::jsonDecode($params)) {
98 1
                $guzzleOptions['json'] = $jsonObject;
99 1
            } else {
100 1
                $guzzleOptions['body'] = $params;
101 1
            }
102
        }
103
104
        return $guzzleOptions;
105 5
    }
106
107 7
    /**
108
     * @param $string
109
     * @return bool|mixed
110
     */
111
    private static function jsonDecode($string)
112
    {
113
        $jsonObject = json_decode($string);
114
        if (json_last_error() == JSON_ERROR_NONE) {
115
            return $jsonObject;
116
        } else {
117
            return false;
118
        }
119
    }
120
}