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

Response   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 41
ccs 0
cts 18
cp 0
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
    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