Completed
Push — master ( 6612be...325c35 )
by Jared
02:19
created

JCRequest::request()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 7.456

Importance

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