Completed
Push — master ( a70f1d...75b407 )
by Ibrahim
06:01
created

RequestBuilder   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 61
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A build() 0 10 1
A packagePayload() 0 10 4
A putArgsIntoEndpoint() 0 6 2
A moveArgsToSentargs() 0 13 4
1
<?php
2
3
namespace Yabacon\Paystack\Http;
4
5
use \Yabacon\Paystack\Contracts\RouteInterface;
6
use \Yabacon\Paystack\Helpers\Router;
7
8
class RequestBuilder
9
{
10
    protected $paystackObj;
11
    protected $interface;
12
    protected $request;
13
14
    public $payload = [ ];
15
    public $sentargs = [ ];
16
17 3
    public function __construct($paystackObj, $interface, array $payload = [ ], array $sentargs = [ ])
18
    {
19 3
        $this->request = new Request($paystackObj);
20 3
        $this->paystackObj = $paystackObj;
21 3
        $this->interface = $interface;
22 3
        $this->payload = $payload;
23 3
        $this->sentargs = $sentargs;
24 3
    }
25
26 1
    public function build()
27
    {
28 1
        $this->request->headers["Authorization"] = "Bearer " . $this->paystackObj->secret_key;
29 1
        $this->request->endpoint = Router::PAYSTACK_API_ROOT . $this->interface[RouteInterface::ENDPOINT_KEY];
30 1
        $this->request->method = $this->interface[RouteInterface::METHOD_KEY];
31 1
        $this->moveArgsToSentargs();
32 1
        $this->putArgsIntoEndpoint($this->request->endpoint);
33 1
        $this->packagePayload();
34 1
        return $this->request;
35
    }
36
37 1
    public function packagePayload()
38
    {
39 1
        if (is_array($this->payload) && count($this->payload)) {
40 1
            if ($this->request->method === RouteInterface::GET_METHOD) {
41 1
                $this->request->endpoint = $this->request->endpoint . '?' . http_build_query($this->payload);
42 1
            } else {
43 1
                $this->request->body = json_encode($this->payload);
44
            }
45 1
        }
46 1
    }
47
48 2
    public function putArgsIntoEndpoint(&$endpoint)
49
    {
50 2
        foreach ($this->sentargs as $key => $value) {
51 2
            $endpoint = str_replace('{' . $key . '}', $value, $endpoint);
52 2
        }
53 2
    }
54
55 2
    public function moveArgsToSentargs()
56
    {
57 2
        if (!array_key_exists(RouteInterface::ARGS_KEY, $this->interface)) {
58 1
            return;
59
        }
60 2
        $args = $this->interface[RouteInterface::ARGS_KEY];
61 2
        foreach ($this->payload as $key => $value) {
62 1
            if (in_array($key, $args)) {
63 1
                $this->sentargs[$key] = $value;
64 1
                unset($this->payload[$key]);
65 1
            }
66 2
        }
67 2
    }
68
}
69