Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
60.00% |
6 / 10 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| SynchronousStreamTransport | |
60.00% |
6 / 10 |
|
0.00% |
0 / 1 |
5.02 | |
0.00% |
0 / 1 |
| request | |
60.00% |
6 / 10 |
|
0.00% |
0 / 1 |
5.02 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Ndrstmr\Icap\Transport; |
| 6 | |
| 7 | use Ndrstmr\Icap\Config; |
| 8 | use Ndrstmr\Icap\Exception\IcapConnectionException; |
| 9 | |
| 10 | /** |
| 11 | * Simple blocking transport using PHP stream sockets. |
| 12 | */ |
| 13 | class SynchronousStreamTransport implements TransportInterface |
| 14 | { |
| 15 | /** |
| 16 | * @return \Amp\Future<string> |
| 17 | */ |
| 18 | public function request(Config $config, string $rawRequest): \Amp\Future |
| 19 | { |
| 20 | $errno = 0; |
| 21 | $errstr = ''; |
| 22 | $address = sprintf('tcp://%s:%d', $config->host, $config->port); |
| 23 | $stream = @stream_socket_client($address, $errno, $errstr, 5); |
| 24 | if ($stream === false) { |
| 25 | throw new IcapConnectionException($errstr ?: 'Connection failed'); |
| 26 | } |
| 27 | |
| 28 | fwrite($stream, $rawRequest); |
| 29 | $response = stream_get_contents($stream); |
| 30 | fclose($stream); |
| 31 | |
| 32 | return \Amp\Future::complete($response !== false ? $response : ''); |
| 33 | } |
| 34 | } |