Passed
Push — master ( 496c70...23eb5d )
by Jared
02:29
created

JCRequest::patch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 7
loc 7
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 4
crap 1
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, $options)
19
    {
20 7
        if (isset($options['params'])) {
21 5
            $params = $options['params'];
22 5
            unset($options['params']);
23
24 5
            if (is_array($params)) {
25 4
                $options['form_params'] = $params;
26 4
            }
27 5
            if (is_string($params)) {
28 1
                $options['json'] = json_decode($params);
29 1
            }
30 5
        }
31
32 7
        return new JCResponse((new Client())->request($method, $url, $options));
33
    }
34
35 1
    public static function get($url, $params = null, $headers = [], $options = [])
36
    {
37 1
        return static::request(Method::GET, is_array($params) ? static::manipulateUrl($url, $params) : $url, [
38
            'headers' => $headers
39 1
        ]);
40
    }
41
42 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...
43
    {
44 2
        return static::request(Method::POST, $url, [
45 2
            'headers' => $headers,
46
            'params' => $params
47 2
        ]);
48
    }
49
50 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...
51
    {
52 1
        return static::request(Method::PUT, $url, [
53 1
            'headers' => $headers,
54
            'params' => $params
55 1
        ]);
56
    }
57
58 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...
59
    {
60 1
        return static::request(Method::PATCH, $url, [
61 1
            'headers' => $headers,
62
            'params' => $params
63 1
        ]);
64
    }
65
66 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...
67
    {
68 1
        return static::request(Method::DELETE, $url, [
69 1
            'headers' => $headers,
70
            'params' => $params
71 1
        ]);
72
    }
73
74 1
    public static function head($url, $params = null, $headers = [], $options = [])
75
    {
76 1
        return static::request(Method::HEAD, $url, [
77 1
            'headers' => $headers,
78 1
        ]);
79
    }
80
81
    /**
82
     * Manipulate the url & params for GET request
83
     *
84
     * @param string $url
85
     * @param array $params
86
     * @return string
87
     */
88 1
    protected static function manipulateUrl($url, $params = [])
89
    {
90 1
        $urlObject = Url::parse($url);
91 1
        $queryData = array_merge($urlObject->query->getData(), $params);
92 1
        $urlObject->query->setData($queryData);
93
94 1
        return $urlObject->getUrl();
95
    }
96
}