Completed
Pull Request — master (#129)
by Eric
257:01 queued 192:06
created

src/ReactHttpAdapter.php (4 issues)

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
 * React http adapter.
23
 *
24
 * @author GeLo <[email protected]>
25
 */
26
class ReactHttpAdapter extends AbstractHttpAdapter
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31 57
    public function getName()
32
    {
33 57
        return 'react';
34 17
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 1558
    protected function sendInternalRequest(InternalRequestInterface $internalRequest)
40
    {
41 1558
        $loop = EventLoopFactory::create();
42 1558
        $dnsResolverFactory = new DnsResolverFactory();
43 1558
        $httpClientFactory = new HttpClientFactory();
44
45 1558
        $error = null;
46 1558
        $response = null;
47 1558
        $body = null;
48
49 1558
        $request = $httpClientFactory->create($loop, $dnsResolverFactory->createCached('8.8.8.8', $loop))->request(
50 1558
            $internalRequest->getMethod(),
51 1558
            $uri = (string) $internalRequest->getUri(),
52 1558
            $this->prepareHeaders($internalRequest, true, true, true)
53 1476
        );
54
55
        $request->on('error', function (\Exception $onError) use (&$error) {
56 38
            $error = $onError;
57 1558
        });
58
59
        $request->on('response', function (Response $onResponse) use (&$response, &$body) {
60 1539
            $onResponse->on('data', function ($data) use (&$body) {
61 1539
                $body .= $data;
62 1539
            });
63
64 1539
            $response = $onResponse;
65 1558
        });
66
67 1558
        $request->end($this->prepareBody($internalRequest));
68 1558
        $loop->run();
69
70 1558
        if ($error !== null) {
71 38
            throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $error->getMessage());
0 ignored issues
show
The method getMessage cannot be called on $error (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...
72
        }
73
74 1539
        return $this->getConfiguration()->getMessageFactory()->createResponse(
75 1539
            (integer) $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...
76 1539
            $response->getVersion(),
0 ignored issues
show
The method getVersion 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...
77 1539
            $response->getHeaders(),
0 ignored issues
show
The method getHeaders 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...
78 1539
            BodyNormalizer::normalize($body, $internalRequest->getMethod())
79 1476
        );
80
    }
81
}
82