Completed
Push — master ( b408c6...9b0fcb )
by Michał
01:56
created

HttpfulTransport::prepare()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
cc 5
eloc 18
nc 7
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
            case HttpMethod::PUT():
23
                $request = $this->put($url, $body);
24
                break;
25
            default:
26
                throw new \Exception("Method not recognised");
27
        }
28
29
        if ($this->isAuthenticated()) {
30
            $request->addHeader("Authorization", "Bearer " . $this->token->getValue());
31
        }
32
      
33
        $request->sends('application/json');
34
        $request->expects('application/json');
35
36
        return $request;
37
    }
38
39
    /**
40
     * @param $url
41
     * @param $body
42
     * @return Request
43
     */
44
    private function post(string $url, string $body)
45
    {
46
        return Request::post($this->host . $url, $body);
47
    }
48
49
    /**
50
     * @param string $url
51
     * @return Request
52
     */
53
    private function get(string $url)
54
    {
55
        return Request::get($this->host . $url);
56
    }
57
58
    /**
59
     * @param string $url
60
     * @param string $body
61
     * @return Request
62
     */
63
    private function put(string $url, string $body)
64
    {
65
        return Request::put($this->host . $url, $body);
66
    }
67
}