Passed
Push — master ( 2c35c6...7904fb )
by Jared
02:33
created

JCRequest::request()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
ccs 12
cts 12
cp 1
rs 9.2
cc 4
eloc 9
nc 5
nop 3
crap 4
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'] = $params;
29 1
            }
30 5
        }
31
32 7
        return new JCResponse((new Client())->request($method, $url, $options));
33
    }
34
35
    /**
36
     * @param $url
37
     * @param array $headers
38
     * @param array $params
39
     * @param array $options
40
     * @return JCResponse
41
     */
42 1
    public static function get($url, $params = null, $headers = [], $options = [])
43
    {
44 1
        return static::request(Method::GET, static::manipulateUrl($url, $params), [
0 ignored issues
show
Bug introduced by
It seems like $params defined by parameter $params on line 42 can also be of type null; however, JC\JCRequest::manipulateUrl() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
45
            'headers' => $headers
46 1
        ]);
47
    }
48
49 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...
50
    {
51 2
        return static::request(Method::POST, $url, [
52 2
            'headers' => $headers,
53
            'params' => $params
54 2
        ]);
55
    }
56
57 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...
58
    {
59 1
        return static::request(Method::PUT, $url, [
60 1
            'headers' => $headers,
61
            'params' => $params
62 1
        ]);
63
    }
64
65 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...
66
    {
67 1
        return static::request(Method::PATCH, $url, [
68 1
            'headers' => $headers,
69
            'params' => $params
70 1
        ]);
71
    }
72
73 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...
74
    {
75 1
        return static::request(Method::DELETE, $url, [
76 1
            'headers' => $headers,
77
            'params' => $params
78 1
        ]);
79
    }
80
81 1
    public static function head($url, $params = null, $headers = [], $options = [])
82
    {
83 1
        return static::request(Method::HEAD, $url, [
84 1
            'headers' => $headers,
85 1
        ]);
86
    }
87
88
    /**
89
     * @param string $url
90
     * @param array $params
91
     * @return string
92
     */
93 1
    protected static function manipulateUrl($url, $params = [])
94
    {
95 1
        $urlObject = Url::parse($url);
96 1
        $queryData = array_merge($urlObject->query->getData(), $params);
97 1
        $urlObject->query->setData($queryData);
98
99 1
        return $urlObject->getUrl();
100
    }
101
}