Completed
Push — master ( 07c80d...24b55a )
by Ibrahim
03:17
created

Response::wrapUp()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

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