1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* File: HalfAsynchronousClient.php |
6
|
|
|
* |
7
|
|
|
* @author Maciej Sławik <[email protected]> |
8
|
|
|
* Github: https://github.com/maciejslawik |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace MSlwk\ReactPhpPlayground\Standalone\Http; |
12
|
|
|
|
13
|
|
|
use MSlwk\ReactPhpPlayground\Api\Data\HtmlInterface; |
14
|
|
|
use MSlwk\ReactPhpPlayground\Api\Data\UrlInterface; |
15
|
|
|
use MSlwk\ReactPhpPlayground\Model\Data\Html; |
16
|
|
|
use MSlwk\ReactPhpPlayground\Model\Adapter\ReactPHP\ClientFactory; |
17
|
|
|
use MSlwk\TypeSafeArray\ObjectArray; |
18
|
|
|
use MSlwk\TypeSafeArray\ObjectArrayFactory; |
19
|
|
|
use React\EventLoop\LoopInterface; |
20
|
|
|
use React\HttpClient\Response; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class HalfAsynchronousClient |
24
|
|
|
* @package MSlwk\ReactPhpPlayground\Standalone\Http |
25
|
|
|
*/ |
26
|
|
|
class HalfAsynchronousClient implements ClientInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var LoopInterface |
30
|
|
|
*/ |
31
|
|
|
private $loop; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ClientFactory |
35
|
|
|
*/ |
36
|
|
|
private $clientFactory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ObjectArrayFactory |
40
|
|
|
*/ |
41
|
|
|
private $objectArrayFactory; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* HalfAsynchronousClient constructor. |
45
|
|
|
* @param LoopInterface $loop |
46
|
|
|
* @param ClientFactory $clientFactory |
47
|
|
|
* @param ObjectArrayFactory $objectArrayFactory |
48
|
|
|
*/ |
49
|
|
|
public function __construct( |
50
|
|
|
LoopInterface $loop, |
51
|
|
|
ClientFactory $clientFactory, |
52
|
|
|
ObjectArrayFactory $objectArrayFactory |
53
|
|
|
) { |
54
|
|
|
$this->loop = $loop; |
55
|
|
|
$this->clientFactory = $clientFactory; |
56
|
|
|
$this->objectArrayFactory = $objectArrayFactory; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param ObjectArray $urls |
61
|
|
|
* @return ObjectArray |
62
|
|
|
*/ |
63
|
|
|
public function getContent(ObjectArray $urls): ObjectArray |
64
|
|
|
{ |
65
|
|
|
$htmlObjectArray = $this->objectArrayFactory->create(HtmlInterface::class); |
66
|
|
|
|
67
|
|
|
for ($i = 0; $i < ($urls->count() / 2); $i++) { |
68
|
|
|
$this->createRequestDefinition($urls->offsetGet($i), $htmlObjectArray); |
69
|
|
|
} |
70
|
|
|
$this->loop->run(); |
71
|
|
|
|
72
|
|
|
for ($i; $i < ($urls->count()); $i++) { |
73
|
|
|
$this->createRequestDefinition($urls->offsetGet($i), $htmlObjectArray); |
74
|
|
|
} |
75
|
|
|
$this->loop->run(); |
76
|
|
|
|
77
|
|
|
return $htmlObjectArray; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @codeCoverageIgnore |
82
|
|
|
* @param UrlInterface $url |
83
|
|
|
* @param ObjectArray $htmlObjectArray |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
private function createRequestDefinition(UrlInterface $url, ObjectArray &$htmlObjectArray): void |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$client = $this->clientFactory->create($this->loop); |
89
|
|
|
$request = $client->request('GET', $url->getUrl()); |
90
|
|
|
$request->on('response', function (Response $response) use (&$htmlObjectArray) { |
91
|
|
|
$data = ''; |
92
|
|
|
$response->on( |
93
|
|
|
'data', |
94
|
|
|
function ($chunk) use (&$data) { |
95
|
|
|
$data .= $chunk; |
96
|
|
|
} |
97
|
|
|
)->on( |
98
|
|
|
'end', |
99
|
|
|
function () use (&$htmlObjectArray, &$data) { |
100
|
|
|
$htmlObjectArray->add(new Html($data)); |
101
|
|
|
} |
102
|
|
|
); |
103
|
|
|
}); |
104
|
|
|
$request->end(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.