Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.73% covered (warning)
72.73%
16 / 22
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
AsyncAmpTransport
72.73% covered (warning)
72.73%
16 / 22
0.00% covered (danger)
0.00%
0 / 1
4.32
0.00% covered (danger)
0.00%
0 / 1
 request
72.73% covered (warning)
72.73%
16 / 22
0.00% covered (danger)
0.00%
0 / 1
4.32
1<?php
2
3declare(strict_types=1);
4
5namespace Ndrstmr\Icap\Transport;
6
7use Amp\Socket;
8use Amp\Socket\ConnectContext;
9use Amp\TimeoutCancellation;
10use Ndrstmr\Icap\Config;
11use Ndrstmr\Icap\Exception\IcapConnectionException;
12
13use function Amp\async;
14
15/**
16 * Asynchronous transport implementation using amphp/socket.
17 */
18final class AsyncAmpTransport implements TransportInterface
19{
20    /**
21     * @return \Amp\Future<string>
22     */
23    public function request(Config $config, string $rawRequest): \Amp\Future
24    {
25        /** @var \Amp\Future<string> $future */
26        $future = async(function () use ($config, $rawRequest): string {
27            $socket = null;
28            $connectionUrl = sprintf('tcp://%s:%d', $config->host, $config->port);
29            $connectContext = (new ConnectContext())
30                ->withConnectTimeout($config->getSocketTimeout());
31            $cancellation = new TimeoutCancellation($config->getStreamTimeout());
32
33            try {
34                $socket = Socket\connect($connectionUrl, $connectContext, $cancellation);
35                $socket->write($rawRequest);
36
37                $response = '';
38                while (null !== ($chunk = $socket->read($cancellation))) {
39                    $response .= $chunk;
40                }
41
42                return $response;
43            } catch (Socket\ConnectException $e) {
44                throw new IcapConnectionException(
45                    sprintf('Async connection to %s:%d failed.', $config->host, $config->port),
46                    0,
47                    $e
48                );
49            } finally {
50                if ($socket) {
51                    $socket->close();
52                }
53            }
54        });
55
56        return $future;
57    }
58}