1 | <?php |
||
11 | class GuzzleClient implements IHttpClient |
||
12 | { |
||
13 | /** |
||
14 | * @var Client |
||
15 | */ |
||
16 | private $guzzleClient; |
||
17 | |||
18 | /** |
||
19 | * @var MountebankExceptionFactory |
||
20 | */ |
||
21 | private $exceptionFactory; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private $host; |
||
27 | |||
28 | /** |
||
29 | * @param Client $client |
||
30 | * @param MountebankExceptionFactory $exceptionFactory |
||
31 | */ |
||
32 | 15 | public function __construct(Client $client, MountebankExceptionFactory $exceptionFactory) |
|
37 | |||
38 | /** |
||
39 | * @return GuzzleClient |
||
40 | */ |
||
41 | 7 | public static function create() |
|
42 | { |
||
43 | 7 | return new self( |
|
44 | 7 | new Client, new MountebankExceptionFactory |
|
45 | 7 | ); |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param string $host |
||
50 | */ |
||
51 | 14 | public function setHost($host) |
|
55 | |||
56 | /** |
||
57 | * @param string $path |
||
58 | * @return string |
||
59 | * @throws MountebankException |
||
60 | */ |
||
61 | 2 | public function get($path) |
|
65 | |||
66 | /** |
||
67 | * @param string $method |
||
68 | * @param string $path |
||
69 | * @param string|null $data |
||
70 | * @return string |
||
71 | * @throws MountebankException |
||
72 | */ |
||
73 | 8 | public function request($method, $path, $data = null) |
|
74 | { |
||
75 | 8 | if (!$this->hasHost()) { |
|
76 | 1 | throw new \LogicException('Host not set; Unable to perform request'); |
|
77 | } |
||
78 | 7 | $options = []; |
|
79 | 7 | if (null !== $data) { |
|
80 | 3 | $options['body'] = $data; |
|
81 | 3 | } |
|
82 | try { |
||
83 | 7 | return $this->guzzleClient |
|
84 | 7 | ->request($method, $this->getUrl($path), $options) |
|
85 | 6 | ->getBody(); |
|
86 | 1 | } catch (ClientException $e) { |
|
87 | 1 | throw $this->convertToMountebankException($e); |
|
88 | } |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @return bool |
||
93 | */ |
||
94 | 8 | public function hasHost() |
|
98 | |||
99 | /** |
||
100 | * @param string $path |
||
101 | * @return string |
||
102 | */ |
||
103 | 7 | private function getUrl($path) |
|
107 | |||
108 | /** |
||
109 | * Creates MountebankException instance from mountebank response |
||
110 | * |
||
111 | * @param ClientException $e |
||
112 | * @return MountebankException |
||
113 | */ |
||
114 | 1 | private function convertToMountebankException(ClientException $e) |
|
115 | { |
||
116 | 1 | return $this->exceptionFactory->createInstanceFromMountebankResponse( |
|
117 | 1 | (string)$e->getResponse()->getBody() |
|
118 | 1 | ); |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param string $path |
||
123 | * @param string $data |
||
124 | * @return string |
||
125 | * @throws MountebankException |
||
126 | */ |
||
127 | 1 | public function post($path, $data) |
|
128 | 1 | { |
|
129 | 1 | return (string)$this->request('POST', $path, $data); |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param string $path |
||
134 | * @param string $data |
||
135 | * @return string |
||
136 | * @throws MountebankException |
||
137 | */ |
||
138 | 1 | public function put($path, $data) |
|
142 | |||
143 | /** |
||
144 | * @param string $path |
||
145 | * @return string |
||
146 | * @throws MountebankException |
||
147 | */ |
||
148 | 1 | public function delete($path) |
|
152 | } |