Completed
Push — master ( 1d240f...6b8a0d )
by Michał
01:54
created

HttpfulTransport::prepare()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 15
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
        $request->sends('application/json');
31
        $request->expects('application/json');
32
33
        return $request;
34
    }
35
36
    /**
37
     * @param $url
38
     * @param $body
39
     * @return Request
40
     */
41
    private function post(string $url, string $body)
42
    {
43
        return Request::post($this->host . $url, $body);
44
    }
45
46
    /**
47
     * @param string $url
48
     * @return Request
49
     */
50
    private function get(string $url)
51
    {
52
        return Request::get($this->host . $url);
53
    }
54
}