1 | <?php |
||
13 | class Juggler |
||
14 | { |
||
15 | const PARAM_REPLAYABLE = 'replayable'; |
||
16 | const PARAM_REMOVE_PROXIES = 'remove_proxies'; |
||
17 | const DEFAULT_PORT = 2525; |
||
18 | |||
19 | /** |
||
20 | * @var IHttpClient |
||
21 | */ |
||
22 | private $httpClient; |
||
23 | |||
24 | /** |
||
25 | * @var AbstractImposterBuilder |
||
26 | */ |
||
27 | private $abstractImposterBuilder; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $host; |
||
33 | |||
34 | /** |
||
35 | * @var int |
||
36 | */ |
||
37 | private $port; |
||
38 | |||
39 | /** |
||
40 | * Full URL to Juggler client |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | private $url; |
||
45 | |||
46 | /** |
||
47 | * @param string $host |
||
48 | * @param int $port |
||
49 | * @param IHttpClient $httpClient |
||
50 | */ |
||
51 | 8 | public function __construct($host, $port = self::DEFAULT_PORT, IHttpClient $httpClient = null) |
|
52 | { |
||
53 | 8 | $this->host = $host; |
|
54 | 8 | $this->port = $port; |
|
55 | 8 | $this->setUrl($host, $port); |
|
56 | 8 | $this->httpClient = isset($httpClient) ? $httpClient : GuzzleClient::create(); |
|
57 | 8 | $this->httpClient->setHost($this->getUrl()); |
|
58 | 8 | $this->abstractImposterBuilder = new AbstractImposterBuilder; |
|
59 | 8 | } |
|
60 | |||
61 | /** |
||
62 | * @param string $host |
||
63 | * @param int $port |
||
64 | */ |
||
65 | 8 | private function setUrl($host, $port) |
|
69 | |||
70 | /** |
||
71 | * @return string |
||
72 | */ |
||
73 | 8 | public function getUrl() |
|
77 | |||
78 | /** |
||
79 | * @return string |
||
80 | */ |
||
81 | 1 | public function getHost() |
|
85 | |||
86 | /** |
||
87 | * @return int |
||
88 | */ |
||
89 | 1 | public function getPort() |
|
93 | |||
94 | /** |
||
95 | * @param string $path |
||
96 | * @return Imposter |
||
97 | */ |
||
98 | 1 | public function createImposterFromFile($path) |
|
102 | |||
103 | /** |
||
104 | * @param string $path |
||
105 | * @throws MountebankException |
||
106 | * @throws \RuntimeException if file does not exist |
||
107 | * @return int Imposter port |
||
108 | */ |
||
109 | public function postImposterFromFile($path) |
||
113 | |||
114 | /** |
||
115 | * @param string $contract |
||
116 | * @throws MountebankException |
||
117 | * @return int Imposter port |
||
118 | */ |
||
119 | 1 | public function postImposterContract($contract) |
|
125 | |||
126 | /** |
||
127 | * @param int $port |
||
128 | * @param bool $replayable |
||
129 | * @param bool $remove_proxies |
||
130 | * @return HttpImposter |
||
131 | */ |
||
132 | public function getHttpImposter($port, $replayable = false, $remove_proxies = false) |
||
136 | |||
137 | /** |
||
138 | * Retrieves contract and builds Imposter |
||
139 | * |
||
140 | * @param int $port |
||
141 | * @param bool $replayable |
||
142 | * @param bool $remove_proxies |
||
143 | * @throws MountebankException |
||
144 | * @return Imposter |
||
145 | */ |
||
146 | 1 | public function getImposter($port, $replayable = false, $remove_proxies = false) |
|
150 | |||
151 | /** |
||
152 | * @param int $port |
||
153 | * @param bool $replayable |
||
154 | * @param bool $remove_proxies |
||
155 | * @throws MountebankException |
||
156 | * @return string |
||
157 | */ |
||
158 | 1 | public function getImposterContract($port, $replayable = false, $remove_proxies = false) |
|
164 | |||
165 | /** |
||
166 | * @param Imposter $imposter |
||
167 | */ |
||
168 | public function replaceImposter(Imposter $imposter) |
||
173 | |||
174 | /** |
||
175 | * @param int|Imposter $imposter Port or Imposter instance |
||
176 | * @param bool $replayable |
||
177 | * @param bool $remove_proxies |
||
178 | * |
||
179 | * @return string Imposter contract |
||
180 | * @throws MountebankException |
||
181 | */ |
||
182 | 3 | public function deleteImposter($imposter, $replayable = false, $remove_proxies = false) |
|
189 | |||
190 | /** |
||
191 | * @param Imposter $imposter |
||
192 | * @throws MountebankException |
||
193 | * @return int Imposter port |
||
194 | */ |
||
195 | 1 | public function postImposter(Imposter $imposter) |
|
196 | { |
||
197 | 1 | $port = $this->postImposterContract(json_encode($imposter)); |
|
198 | 1 | if (!$imposter->hasPort()) { |
|
199 | 1 | $imposter->setPort($port); |
|
200 | 1 | } |
|
201 | |||
202 | 1 | return $port; |
|
203 | } |
||
204 | |||
205 | /** |
||
206 | * @throws MountebankException |
||
207 | */ |
||
208 | 1 | public function deleteImposters() |
|
212 | |||
213 | /** |
||
214 | * @param int $port |
||
215 | * @throws MountebankException |
||
216 | */ |
||
217 | 1 | public function removeProxies($port) |
|
222 | |||
223 | /** |
||
224 | * Retrieves imposter contract and saves to a local filesystem |
||
225 | * |
||
226 | * @param int|Imposter $imposter |
||
227 | * @param string $path |
||
228 | */ |
||
229 | public function saveContract($imposter, $path) |
||
234 | |||
235 | /** |
||
236 | * mountebank API only supports string 'true' as boolean param value |
||
237 | * |
||
238 | * @param bool $replayable |
||
239 | * @param bool $remove_proxies |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | 5 | private function composeQueryString($replayable, $remove_proxies) |
|
250 | } |
||
251 |