Passed
Push — master ( 8cd1cf...370b72 )
by Jasper
03:04
created

DocumentClient::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Swis\JsonApi\Client;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Swis\JsonApi\Client\Interfaces\ClientInterface;
7
use Swis\JsonApi\Client\Interfaces\DocumentClientInterface;
8
use Swis\JsonApi\Client\Interfaces\DocumentInterface;
9
use Swis\JsonApi\Client\Interfaces\ItemDocumentInterface;
10
use Swis\JsonApi\Client\Interfaces\ParserInterface;
11
12
class DocumentClient implements DocumentClientInterface
13
{
14
    /**
15
     * @var \Swis\JsonApi\Client\Interfaces\ClientInterface
16
     */
17
    private $client;
18
19
    /**
20
     * @var \Swis\JsonApi\Client\Interfaces\ParserInterface
21
     */
22
    private $parser;
23
24
    /**
25
     * @param \Swis\JsonApi\Client\Interfaces\ClientInterface $client
26
     * @param \Swis\JsonApi\Client\Interfaces\ParserInterface $parser
27
     */
28 25
    public function __construct(ClientInterface $client, ParserInterface $parser)
29
    {
30 25
        $this->client = $client;
31 25
        $this->parser = $parser;
32 25
    }
33
34
    /**
35
     * @return string
36
     */
37 5
    public function getBaseUri(): string
38
    {
39 5
        return $this->client->getBaseUri();
40
    }
41
42
    /**
43
     * @param string $baseUri
44
     */
45 5
    public function setBaseUri(string $baseUri)
46
    {
47 5
        $this->client->setBaseUri($baseUri);
48 5
    }
49
50
    /**
51
     * @param string $endpoint
52
     * @param array  $headers
53
     *
54
     * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
55
     */
56 5
    public function get(string $endpoint, array $headers = []): DocumentInterface
57
    {
58 5
        return $this->parseResponse($this->client->get($endpoint, $headers));
59
    }
60
61
    /**
62
     * @param string                                                $endpoint
63
     * @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $body
64
     * @param array                                                 $headers
65
     *
66
     * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
67
     */
68 5
    public function post(string $endpoint, ItemDocumentInterface $body, array $headers = []): DocumentInterface
69
    {
70 5
        return $this->parseResponse($this->client->post($endpoint, $this->prepareBody($body), $headers));
71
    }
72
73
    /**
74
     * @param string                                                $endpoint
75
     * @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $body
76
     * @param array                                                 $headers
77
     *
78
     * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
79
     */
80 5
    public function patch(string $endpoint, ItemDocumentInterface $body, array $headers = []): DocumentInterface
81
    {
82 5
        return $this->parseResponse($this->client->patch($endpoint, $this->prepareBody($body), $headers));
83
    }
84
85
    /**
86
     * @param string $endpoint
87
     * @param array  $headers
88
     *
89
     * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
90
     */
91 5
    public function delete(string $endpoint, array $headers = []): DocumentInterface
92
    {
93 5
        return $this->parseResponse($this->client->delete($endpoint, $headers));
94
    }
95
96
    /**
97
     * @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $body
98
     *
99
     * @return string
100
     */
101 10
    protected function prepareBody(ItemDocumentInterface $body): string
102
    {
103 10
        return $this->sanitizeJson(json_encode($body));
104
    }
105
106
    /**
107
     * @param string $json
108
     *
109
     * @return string
110
     */
111 10
    protected function sanitizeJson(string $json): string
112
    {
113 10
        return str_replace('\r\n', '\\n', $json);
114
    }
115
116
    /**
117
     * @param \Psr\Http\Message\ResponseInterface $response
118
     *
119
     * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
120
     */
121 20
    protected function parseResponse(ResponseInterface $response): DocumentInterface
122
    {
123 20
        return $this->parser->deserializeResponse($response);
124
    }
125
}
126