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

Response   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 41
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parsePaystackResponse() 0 13 4
A implodedMessages() 0 4 1
B wrapUp() 0 14 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