ReactHttpAdapter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 11
dl 0
loc 56
ccs 31
cts 31
cp 1
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 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 Eric GELOEN
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 GeLo
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 Eric GELOEN
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 Eric GELOEN
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