Completed
Pull Request — master (#2)
by Michał
01:58
created

HttpfulTransport::post()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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