ReactHttpAdapter::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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());
0 ignored issues
show
Bug introduced by
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...
70
        }
71
72 729
        return $this->getConfiguration()->getMessageFactory()->createResponse(
73 729
            (int) $response->getCode(),
0 ignored issues
show
Bug introduced by
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(),
0 ignored issues
show
Bug introduced by
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...
75 736
            $response->getHeaders(),
0 ignored issues
show
Bug introduced by
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...
76 729
            BodyNormalizer::normalize($body, $internalRequest->getMethod())
77 567
        );
78
    }
79
}
80