Completed
Push — master ( 893fe7...1d240f )
by Michał
01:59
created

HttpfulTransport::prepare()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
cc 4
eloc 13
nc 5
nop 3
1
<?php
2
namespace Barenote\Transport;
3
4
use Barenote\Enum\HttpMethod;
5
use Httpful\Request;
6
7
/**
8
 * Class HttpfulTransport
9
 * @package Barenote\Transport
10
 */
11
class HttpfulTransport extends BaseTransport
12
{
13
    public function prepare(HttpMethod $method, string $url, string $body = ""): Request
14
    {
15
        switch ($method) {
16
            case HttpMethod::POST():
17
                $request = $this->post($url, $body);
18
                break;
19
            case HttpMethod::GET():
20
                $request = $this->get($url);
21
                break;
22
            default:
23
                throw new \Exception("Method not recognised");
24
        }
25
26
        if ($this->isAuthenticated()) {
27
            $request->addHeader("Authorization", "Bearer " . $this->token->getValue());
28
        }
29
30
        return $request;
31
    }
32
33
    /**
34
     * @param $url
35
     * @param $body
36
     * @return Request
37
     */
38
    private function post(string $url, string $body)
39
    {
40
        return Request::post($this->host . $url, $body);
41
    }
42
43
    /**
44
     * @param string $url
45
     * @return Request
46
     */
47
    private function get(string $url)
48
    {
49
        return Request::get($this->host . $url);
50
    }
51
}