Completed
Pull Request — master (#131)
by Eric
63:39 queued 61:20
created

ReactHttpAdapter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 11
dl 0
loc 56
ccs 0
cts 31
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
B sendInternalRequest() 0 42 2
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
    public function getName()
30
    {
31
        return 'react';
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    protected function sendInternalRequest(InternalRequestInterface $internalRequest)
38
    {
39
        $loop = EventLoopFactory::create();
40
        $dnsResolverFactory = new DnsResolverFactory();
41
        $httpClientFactory = new HttpClientFactory();
42
43
        $error = null;
44
        $response = null;
45
        $body = null;
46
47
        $request = $httpClientFactory->create($loop, $dnsResolverFactory->createCached('8.8.8.8', $loop))->request(
48
            $internalRequest->getMethod(),
49
            $uri = (string) $internalRequest->getUri(),
50
            $this->prepareHeaders($internalRequest, true, true, true)
51
        );
52
53
        $request->on('error', function (\Exception $onError) use (&$error) {
54
            $error = $onError;
55
        });
56
57
        $request->on('response', function (Response $onResponse) use (&$response, &$body) {
58
            $onResponse->on('data', function ($data) use (&$body) {
59
                $body .= $data;
60
            });
61
62
            $response = $onResponse;
63
        });
64
65
        $request->end($this->prepareBody($internalRequest));
66
        $loop->run();
67
68
        if ($error !== null) {
69
            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
        return $this->getConfiguration()->getMessageFactory()->createResponse(
73
            (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
            $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
            $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
            BodyNormalizer::normalize($body, $internalRequest->getMethod())
77
        );
78
    }
79
}
80