Completed
Push — master ( 890e48...201e88 )
by Ibrahim
02:37
created

Response::wrapUp()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 9
cp 0
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 9
nc 4
nop 0
crap 42
1
<?php
2
3
namespace Yabacon\Paystack\Http;
4
5
use \Yabacon\Paystack\Exception\ApiException;
6
7
class Response
8
{
9
    public $okay;
10
    public $body;
11
    public $forApi;
12
    public $messages = [];
13
14
    private function parsePaystackResponse()
15
    {
16
        $resp = \json_decode($this->body);
17
18
        if (json_last_error() !== JSON_ERROR_NONE || !$resp->status) {
19
            throw new ApiException(
20
                "Paystack Request failed with response: '" .
21
                ((json_last_error() === JSON_ERROR_NONE) ? $resp->message : $this->body) . "'."
22
            );
23
        }
24
25
        return $resp;
26
    }
27
28
    private function implodedMessages()
29
    {
30
        return implode("\n\n", $this->messages);
31
    }
32
33
    public function wrapUp()
34
    {
35
        if ($this->okay && $this->forApi) {
36
            return $this->parsePaystackResponse();
37
        }
38
        if (!$this->okay && $this->forApi) {
39
            throw new Exception($this->implodedMessages());
40
        }
41
        if ($this->okay) {
42
            error_log($this->implodedMessages());
43
            return $this->body;
44
        }
45
        return false;
46
    }
47
}
48