Completed
Pull Request — master (#7)
by Michał
02:49 queued 51s
created

HttpfulTransport   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 57
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B prepare() 0 25 5
A post() 0 4 1
A get() 0 4 1
A put() 0 4 1
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
}