1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* File: AsynchronousClient.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 AsynchronousClient |
24
|
|
|
* @package MSlwk\ReactPhpPlayground\Standalone\Http |
25
|
|
|
*/ |
26
|
|
|
class AsynchronousClient 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
|
|
|
* AsynchronousClient 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
|
|
View Code Duplication |
public function getContent(ObjectArray $urls): ObjectArray |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$htmlObjectArray = $this->objectArrayFactory->create(HtmlInterface::class); |
66
|
|
|
/** @var UrlInterface $url */ |
67
|
|
|
foreach ($urls as $url) { |
68
|
|
|
$this->createRequestDefinition($url, $htmlObjectArray); |
69
|
|
|
} |
70
|
|
|
$this->loop->run(); |
71
|
|
|
return $htmlObjectArray; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @codeCoverageIgnore |
76
|
|
|
* @param UrlInterface $url |
77
|
|
|
* @param ObjectArray $htmlObjectArray |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
private function createRequestDefinition(UrlInterface $url, ObjectArray &$htmlObjectArray): void |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$client = $this->clientFactory->create($this->loop); |
83
|
|
|
$request = $client->request('GET', $url->getUrl()); |
84
|
|
|
$request->on('response', function (Response $response) use (&$htmlObjectArray) { |
85
|
|
|
$data = ''; |
86
|
|
|
$response->on( |
87
|
|
|
'data', |
88
|
|
|
function ($chunk) use (&$data) { |
89
|
|
|
$data .= $chunk; |
90
|
|
|
} |
91
|
|
|
)->on( |
92
|
|
|
'end', |
93
|
|
|
function () use (&$htmlObjectArray, &$data) { |
94
|
|
|
$htmlObjectArray->add(new Html($data)); |
95
|
|
|
} |
96
|
|
|
); |
97
|
|
|
}); |
98
|
|
|
$request->end(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.