JsonTransporter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 53
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parseResponseToData() 0 3 1
A parseResponseStringToObject() 0 3 1
A setHeaderOnRequest() 0 5 1
A setRequestBody() 0 3 1
1
<?php
2
3
namespace Trucker\Transporters;
4
5
use Guzzle\Http\Message\Request;
6
use Guzzle\Http\Message\Response;
7
use Trucker\Requests\RestRequest;
8
9
class JsonTransporter implements TransporterInterface
10
{
11
    /**
12
     * Function to set the appropriate headers on a request object
13
     * to facilitate a JSON transport.
14
     *
15
     * @param Request $request
16
     */
17 37
    public function setHeaderOnRequest(Request $request)
18
    {
19 37
        $request->setHeaders([
20 37
            'Accept' => 'application/json',
21
            'Content-Type' => 'application/json',
22
        ]);
23 37
    }
24
25
    /**
26
     * Function to convert a response object into an associative
27
     * array of data.
28
     *
29
     * @param Response $response
30
     *
31
     * @return array
32
     *
33
     * @throws \Guzzle\Common\Exception\RuntimeException
34
     */
35 12
    public function parseResponseToData(Response $response)
36
    {
37 12
        return $response->json();
38
    }
39
40
    /**
41
     * Function to parse the response string into an object
42
     * specific to JSON.
43
     *
44
     * @param Response $response
45
     *
46
     * @return mixed
47
     */
48 8
    public function parseResponseStringToObject(Response $response)
49
    {
50 8
        return json_decode($response->getBody(true));
51
    }
52
53
    /**
54
     * Set the request body for the given request.
55
     *
56
     * @param RestRequest $request
57
     * @param             $body
58
     */
59 6
    public function setRequestBody(RestRequest $request, $body)
60
    {
61 6
        $request->setBody(json_encode($body), 'application/json');
62 6
    }
63
}
64