1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* File: SynchronousClient.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\TypeSafeArray\ObjectArray; |
17
|
|
|
use MSlwk\TypeSafeArray\ObjectArrayFactory; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class SynchronousClient |
21
|
|
|
* @package MSlwk\ReactPhpPlayground\Standalone\Http |
22
|
|
|
*/ |
23
|
|
|
class SynchronousClient implements ClientInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var ObjectArrayFactory |
27
|
|
|
*/ |
28
|
|
|
private $objectArrayFactory; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* SynchronousClient constructor. |
32
|
|
|
* @param ObjectArrayFactory $objectArrayFactory |
33
|
|
|
*/ |
34
|
|
|
public function __construct(ObjectArrayFactory $objectArrayFactory) |
35
|
|
|
{ |
36
|
|
|
$this->objectArrayFactory = $objectArrayFactory; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param ObjectArray $urls |
41
|
|
|
* @return ObjectArray |
42
|
|
|
*/ |
43
|
|
View Code Duplication |
public function getContent(ObjectArray $urls): ObjectArray |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$htmlObjectArray = $this->objectArrayFactory->create(HtmlInterface::class); |
46
|
|
|
/** @var UrlInterface $url */ |
47
|
|
|
foreach ($urls as $url) { |
48
|
|
|
$html = $this->fileGetContentsWrapper($url); |
49
|
|
|
$htmlObjectArray->add(new Html($html)); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
return $htmlObjectArray; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @codeCoverageIgnore |
56
|
|
|
* @param UrlInterface $url |
57
|
|
|
* @return bool|string |
58
|
|
|
*/ |
59
|
|
|
protected function fileGetContentsWrapper(UrlInterface $url) |
60
|
|
|
{ |
61
|
|
|
$html = file_get_contents($url->getUrl()); |
62
|
|
|
return $html; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
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.