Completed
Push — master ( 45bee2...97600c )
by Jasper
07:14
created

DocumentClient::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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