Completed
Push — master ( 7b285b...480624 )
by Rigel Kent
04:14
created

Request::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Luigel\Paymongo\Traits;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\ClientException;
7
use Illuminate\Database\Eloquent\Model;
8
use Luigel\Paymongo\Exceptions\BadRequestException;
9
use Luigel\Paymongo\Exceptions\NotFoundException;
10
use Luigel\Paymongo\Exceptions\PaymentErrorException;
11
use Luigel\Paymongo\Models\Webhook;
12
13
trait Request
14
{
15
    protected $data;
16
    protected $options;
17
18
    /**
19
     * Request a create to API
20
     *
21
     * @param array $payload
22
     * @return Model
23
     */
24
    public function create($payload)
25
    {
26
        $this->method = 'POST';
0 ignored issues
show
Bug introduced by
The property method does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
        $this->payload = $this->convertPayloadAmountsToInteger($payload);
0 ignored issues
show
Bug introduced by
The property payload does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28
        $this->formRequestData();
29
30
        $this->setOptions([
31
            'headers' => [
32
                'Accept' => 'application/json',
33
                'Content-type' => 'application/json'
34
            ],
35
            'auth' => [config('paymongo.secret_key'), ''],
36
            'json' => $this->data,
37
        ]);
38
39
        return $this->request();
40
    }
41
42
    /**
43
     * Request a create to API
44
     *
45
     * @param array $payload
46
     * @return Model
47
     */
48 View Code Duplication
    public function find($payload)
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...
49
    {
50
        $this->method = 'GET';
51
        $this->payload = $payload;
52
        $this->apiUrl = $this->apiUrl . $payload;
0 ignored issues
show
Bug introduced by
The property apiUrl does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
53
54
        $this->setOptions([
55
            'headers' => [
56
                'Accept' => 'application/json',
57
                'Content-type' => 'application/json'
58
            ],
59
            'auth' => [config('paymongo.secret_key'), ''],
60
        ]);
61
62
        return $this->request();
63
    }
64
65
    public function all()
66
    {
67
        $this->method = 'GET';
68
69
        $this->setOptions([
70
            'headers' => [
71
                'Accept' => 'application/json',
72
                'Content-type' => 'application/json'
73
            ],
74
            'auth' => [config('paymongo.secret_key'), ''],
75
        ]);
76
77
        return $this->request();
78
    }
79
80 View Code Duplication
    public function enable(Webhook $webhook)
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...
81
    {
82
        $this->method = 'POST';
83
        $this->apiUrl = $this->apiUrl . "$webhook->id/enable";
84
85
        $this->setOptions([
86
            'headers' => [
87
                'Accept' => 'application/json',
88
            ],
89
            'auth' => [config('paymongo.secret_key'), ''],
90
        ]);
91
92
        return $this->request();
93
    }
94
95 View Code Duplication
    public function disable(Webhook $webhook)
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...
96
    {
97
        $this->method = 'POST';
98
        $this->apiUrl = $this->apiUrl . "$webhook->id/disable";
99
100
        $this->setOptions([
101
            'headers' => [
102
                'Accept' => 'application/json',
103
            ],
104
            'auth' => [config('paymongo.secret_key'), ''],
105
        ]);
106
107
        return $this->request();
108
    }
109
110
    public function update(Webhook $webhook, $payload)
111
    {
112
        $this->method = 'PUT';
113
        $this->payload = $this->convertPayloadAmountsToInteger($payload);
114
        $this->apiUrl = $this->apiUrl . $webhook->id;
115
116
        $this->formRequestData();
117
        $this->setOptions([
118
            'headers' => [
119
                'Accept' => 'application/json',
120
            ],
121
            'auth' => [config('paymongo.secret_key'), ''],
122
            'json' => $this->data,
123
        ]);
124
125
        return $this->request();
126
    }
127
128
    /**
129
     * Send request to API
130
     *
131
     * @return mixed|Throwable
132
     */
133
    protected function request()
134
    {
135
        $client = new Client();
136
137
        try
138
        {
139
            $response = $client->request($this->method, $this->apiUrl, $this->options);
140
141
            $array = $this->parseToArray((string) $response->getBody());
142
            return $this->setReturnModel($array);
143
        }
144
        catch (ClientException $e)
145
        {
146
            $response = json_decode($e->getResponse()->getBody()->getContents(), true);
147
            if ($e->getCode() === 400)
148
            {
149
                throw new BadRequestException($response['errors'][0]['detail']);
150
            }
151
            else if ($e->getCode() === 402)
152
            {
153
                throw new PaymentErrorException($response['errors'][0]['detail']);
154
            }
155
            else if ($e->getCode() === 404)
156
            {
157
                throw new NotFoundException($response['errors'][0]['detail']);
158
            }
159
        }
160
161
162
163
    }
164
165
    protected function formRequestData()
166
    {
167
        $this->data = [
168
            'data' => [
169
                'attributes' => $this->payload
170
            ]
171
        ];
172
    }
173
174
    protected function parseToArray($jsonString)
175
    {
176
        return json_decode($jsonString, true);
177
    }
178
179
    protected function setReturnModel($array)
180
    {
181
        return (new $this->returnModel)->setData($array['data']);
0 ignored issues
show
Bug introduced by
The property returnModel does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
182
    }
183
184
    protected function setOptions($options)
185
    {
186
        $this->options = $options;
187
    }
188
189
    protected function convertPayloadAmountsToInteger($payload)
190
    {
191
        if (isset($payload['amount']))
192
        {
193
            $payload['amount'] *= 100;
194
        }
195
        return $payload;
196
197
    }
198
}
199