-
Notifications
You must be signed in to change notification settings - Fork 3
/
Client.php
144 lines (115 loc) · 4.08 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace Staffim\RestClient;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\HttpKernel\KernelInterface;
class Client
{
/**
* @var string
*/
private $baseUrl;
/**
* @var \Symfony\Component\HttpKernel\KernelInterface
*/
private $kernel;
/**
* @var KernelBrowser
*/
private $kernelBrowser;
public function __construct(KernelInterface $kernel, $baseUrl, array $headers = [])
{
$this->kernel = $kernel;
$this->baseUrl = $baseUrl;
$this->kernelBrowser = $this->createBrowser($kernel, $headers);
}
public function post($data, $statusCode = 201, array $options = []): Response
{
$this->kernelBrowser->request(
'POST',
$this->buildUrlFromOptions($options),
$options['parameters'] ?? [],
$options['files'] ?? [],
$options['server'] ?? [],
$this->prepareContent($data)
);
return $this->createResponse($statusCode);
}
public function put($data, $statusCode = 200, array $options = []): Response
{
$additionalPath = is_array($data) && array_key_exists('id', $data) ? [$data['id']] : [];
$this->kernelBrowser->request(
'PUT',
$this->buildUrlFromOptions($options, $additionalPath),
$options['parameters'] ?? [],
$options['files'] ?? [],
$options['server'] ?? [],
$this->prepareContent($data)
);
return $this->createResponse($statusCode);
}
public function patch($id, array $operations = [], $statusCode = 200, array $options = []): Response
{
$this->kernelBrowser->request(
'PATCH',
$this->buildUrlFromOptions($options, [$id]),
$options['parameters'] ?? [],
$options['files'] ?? [],
$options['server'] ?? [],
$this->prepareContent($operations)
);
return $this->createResponse($statusCode);
}
public function get($id, $statusCode = 200, array $options = []): Response
{
$this->kernelBrowser->request('GET', $this->buildUrlFromOptions($options, [$id]));
return $this->createResponse($statusCode);
}
public function delete($id, $statusCode = 204, array $options = [])
{
$this->kernelBrowser->request('DELETE', $this->buildUrlFromOptions($options, [$id]));
return $this->createResponse($statusCode);
}
public function getList(array $parameters = [], $statusCode = 200, array $options = []): Response
{
$options['query'] = array_merge($options['query'] ?? [], $parameters);
$this->kernelBrowser->request('GET', $this->buildUrlFromOptions($options, []));
return $this->createResponse($statusCode);
}
/**
* @return KernelBrowser
*/
public function getKernelBrowser(): KernelBrowser
{
return $this->kernelBrowser;
}
private function createResponse($statusCode = 200): Response
{
$response = $this->kernelBrowser->getResponse();
if ($statusCode !== $response->getStatusCode()) {
throw new RestException($this->kernelBrowser);
}
return new Response($response);
}
private function createBrowser(KernelInterface $kernel, array $headers = []): KernelBrowser
{
$browser = $kernel->getContainer()->get('test.client');
$browser->setServerParameters($headers);
return $browser;
}
private function buildUrlFromOptions(array $options = [], array $additionalPath = []): string
{
return $this->buildUrl(array_merge($additionalPath, $options['extraUrl'] ?? []), $options['query'] ?? []);
}
private function buildUrl(array $parts = [], array $parameters = []): string
{
$url = implode('/', array_merge([$this->baseUrl], $parts));
if ($parameters) {
$url .= '?' . http_build_query($parameters);
}
return $url;
}
private function prepareContent($data): string
{
return is_array($data) ? json_encode($data) : $data;
}
}