Request::post()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
c 0
b 0
f 0
rs 9.9332
cc 2
nc 2
nop 2
1
<?php
2
3
namespace MacsiDigital\Xero\Support;
4
5
use Exception;
6
use GuzzleHttp\Client;
7
use GuzzleHttp\HandlerStack;
8
use GuzzleHttp\Subscriber\Oauth\Oauth1;
9
10
class Request
11
{
12
    protected $client;
13
14
    public function bootPrivateApplication()
15
    {
16
        $middleware = new Oauth1([
17
            'consumer_key' => config('xero.oauth.consumer_key'),
18
            'token' => config('xero.oauth.consumer_key'),
19
            'private_key_file' => storage_path(config('xero.oauth.rsa_private_key')),
20
            'private_key_passphrase' => config('xero.oauth.rsa_private_key_passphrase'),
21
            'signature_method' => Oauth1::SIGNATURE_METHOD_RSA,
22
        ]);
23
24
        $stack = HandlerStack::create();
25
        $stack->push($middleware);
26
27
        $options = [
28
            'base_uri' => 'https://api.xero.com/api.xro/2.0/',
29
            'handler' => $stack,
30
            'auth' => 'oauth',
31
            'headers' => [
32
                'Accept' => 'application/json',
33
                'Content-type' => 'application/json',
34
            ],
35
        ];
36
37
        $this->client = new Client($options);
38
39
        return $this;
40
    }
41
42
    public function get($end_point)
43
    {
44
        try {
45
            return $this->client->request('GET', $end_point);
46
        } catch (Exception $e) {
47
            return $e->getResponse();
48
        }
49
    }
50
51 View Code Duplication
    public function post($end_point, $fields)
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...
52
    {
53
        try {
54
            return $this->client->post($end_point, [
55
                'body' => $this->prepareFields($fields),
56
            ]);
57
        } catch (Exception $e) {
58
            return $e->getResponse();
59
        }
60
    }
61
62 View Code Duplication
    public function put($end_point, $fields)
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
        try {
65
            return $this->client->put($end_point, [
66
                'body' => $this->prepareFields($fields),
67
            ]);
68
        } catch (Exception $e) {
69
            return $e->getResponse();
70
        }
71
    }
72
73
    public function delete($end_point)
74
    {
75
        try {
76
            return $this->client->delete($end_point);
77
        } catch (Exception $e) {
78
            return $e->getResponse();
79
        }
80
    }
81
82
    private function prepareFields($fields)
83
    {
84
        $return = [];
85
        foreach ($fields as $key => $value) {
86
            if ($value != [] && $value != '') {
87
                if (is_array($value)) {
88
                    foreach ($value as $sub_key => $object) {
89
                        if (is_object($object)) {
90 View Code Duplication
                            if (is_array($fields[$key][$sub_key])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
91
                                $return[$key][$sub_key][] = $object->getAttributes();
92
                            } else {
93
                                $return[$key][$sub_key] = $object->getAttributes();
94
                            }
95 View Code Duplication
                        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
96
                            if (is_array($fields[$key][$sub_key])) {
97
                                $return[$key][$sub_key][] = $object;
98
                            } else {
99
                                $return[$key][$sub_key] = $object;
100
                            }
101
                        }
102
                    }
103
                } else {
104
                    if (is_object($value)) {
105 View Code Duplication
                        if (is_array($fields[$key])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
106
                            $return[$key][] = $value->getAttributes();
107
                        } else {
108
                            $return[$key] = $value->getAttributes();
109
                        }
110 View Code Duplication
                    } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
111
                        if (is_array($fields[$key])) {
112
                            $return[$key][] = $value;
113
                        } else {
114
                            $return[$key] = $value;
115
                        }
116
                    }
117
                }
118
            }
119
        }
120
121
        return json_encode($return);
122
    }
123
}
124