AsyncHttpClientDefault::send()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: christian
5
 * Date: 12/24/16
6
 * Time: 8:12 PM
7
 */
8
9
namespace AsyncHttpClient\Core;
10
11
12
use AsyncHttpClient\Logger\AsyncHttpLogger;
13
use AsyncHttpClient\Service\AsyncHttpService;
14
use React\EventLoop\LoopInterface;
15
use React\HttpClient\Client as ReactClient;
16
use React\HttpClient\Request;
17
use React\HttpClient\Response;
18
19
class AsyncHttpClientDefault implements AsyncHttpClient
20
{
21
22
    /**
23
     * @var AsyncHttpService[]
24
     */
25
    private $services = [];
26
27
    /**
28
     * @var ReactClient
29
     */
30
    private $client;
31
32
    /**
33
     * @var LoopInterface
34
     */
35
    private $loop;
36
37
    /**
38
     * @var AsyncHttpLogger
39
     */
40
    private $logger;
41
42
    /**
43
     * AsyncServiceExecutor constructor.
44
     *
45
     * @param ReactClient     $client
46
     * @param LoopInterface   $loop
47
     * @param AsyncHttpLogger $logger
48
     */
49 1
    public function __construct(ReactClient $client, LoopInterface $loop, AsyncHttpLogger $logger)
50
    {
51 1
        $this->client = $client;
52 1
        $this->loop   = $loop;
53 1
        $this->logger = $logger;
54 1
    }
55
56
57 1
    public function addService(AsyncHttpService $service)
58
    {
59 1
        $this->services[] = $service;
60 1
    }
61
62 1
    public function send()
63
    {
64 1
        $startTime = microtime(true);
65
66 1
        while (count($this->services) > 0) {
67 1
            $service = array_shift($this->services);
68 1
            $request = $this->createRequest($service);
69
70 1
            $this->defineHandlers($request, $service);
71
72 1
            $request->end();
73 1
        }
74
75 1
        $this->loop->run();
76
77 1
        $this->logger->logTotal($startTime);
78 1
    }
79
80 1
    private function createRequest(AsyncHttpService $service)
81
    {
82 1
        $request = $this->client->request($service->getMethod(), $service->getUrl(), $service->getHeaders());
83
        $request->write($service->getContent());
84
        
85 1
        return $request;
86
    }
87 1
88
    private function defineHandlers(Request $request, AsyncHttpService $service)
89
    {
90
        $start = microtime(true);
91 1
92
        $request->on('response', function (Response $response) use ($start, $service) {
93 1
94
            $response->on('data', function ($data, Response $response) use ($start, $service) {
95 1
96 1
                $service->execute($data, $response);
97 1
98
                $this->logger->log($service->getMethod(), $service->getUrl(), $data, $start);
99
            }
100
            );
101 1
102 1
            // TODO: Implements error case
103 1
            // $response->on('error', ...);
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
104
        }
105
        );
106
    }
107
}
108