Completed
Push — master ( c8f54d...0a9660 )
by Eric
21:26 queued 01:31
created

src/ReactHttpAdapter.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the Ivory Http Adapter package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\HttpAdapter;
13
14
use Ivory\HttpAdapter\Message\InternalRequestInterface;
15
use Ivory\HttpAdapter\Normalizer\BodyNormalizer;
16
use React\Dns\Resolver\Factory as DnsResolverFactory;
17
use React\EventLoop\Factory as EventLoopFactory;
18
use React\HttpClient\Factory as HttpClientFactory;
19
use React\HttpClient\Response;
20
21
/**
22
 * @author GeLo <[email protected]>
23
 */
24
class ReactHttpAdapter extends AbstractHttpAdapter
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 27
    public function getName()
30
    {
31 27
        return 'react';
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 738
    protected function sendInternalRequest(InternalRequestInterface $internalRequest)
38
    {
39 738
        $loop = EventLoopFactory::create();
40 738
        $dnsResolverFactory = new DnsResolverFactory();
41 738
        $httpClientFactory = new HttpClientFactory();
42
43 738
        $error = null;
44 738
        $response = null;
45 738
        $body = null;
46
47 738
        $request = $httpClientFactory->create($loop, $dnsResolverFactory->createCached('8.8.8.8', $loop))->request(
48 738
            $internalRequest->getMethod(),
49 738
            $uri = (string) $internalRequest->getUri(),
50 738
            $this->prepareHeaders($internalRequest, true, true, true)
51 574
        );
52
53
        $request->on('error', function (\Exception $onError) use (&$error) {
54 18
            $error = $onError;
55 738
        });
56
57
        $request->on('response', function (Response $onResponse) use (&$response, &$body) {
58 729
            $onResponse->on('data', function ($data) use (&$body) {
59 729
                $body .= $data;
60 729
            });
61
62 729
            $response = $onResponse;
63 738
        });
64
65 738
        $request->end($this->prepareBody($internalRequest));
66 738
        $loop->run();
67
68 738
        if ($error !== null) {
69 18
            throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $error->getMessage());
70
        }
71
72 729
        return $this->getConfiguration()->getMessageFactory()->createResponse(
73 729
            (int) $response->getCode(),
0 ignored issues
show
The method getCode cannot be called on $response (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
74 729
            $response->getVersion(),
75 736
            $response->getHeaders(),
76 729
            BodyNormalizer::normalize($body, $internalRequest->getMethod())
77 567
        );
78
    }
79
}
80