ReactPHPRequestAdapter::validateWebpages()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * File: ReactPHPRequestAdapter.php
4
 *
5
 * @author      Maciej Sławik <[email protected]>
6
 * Github:      https://github.com/maciejslawik
7
 */
8
9
namespace MSlwk\Otomoto\Middleware\Webpage\Adapter\ReactPHP;
10
11
use MSlwk\Otomoto\Middleware\Webpage\Adapter\ReactPHP\Handler\ErrorHandler;
0 ignored issues
show
Bug introduced by
The type MSlwk\Otomoto\Middleware...HP\Handler\ErrorHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use MSlwk\Otomoto\Middleware\Webpage\Adapter\ReactPHP\Handler\ResponseHandler;
0 ignored issues
show
Bug introduced by
The type MSlwk\Otomoto\Middleware...Handler\ResponseHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use MSlwk\Otomoto\Middleware\Webpage\Data\UrlDTOArray;
14
use MSlwk\Otomoto\Middleware\Webpage\Data\WebpageDTO;
15
use MSlwk\Otomoto\Middleware\Webpage\Data\WebpageDTOArray;
16
use MSlwk\Otomoto\Middleware\Webpage\Exception\GETHandlerException;
17
use React\EventLoop\LoopInterface;
18
use React\HttpClient\Client;
19
use React\HttpClient\Response;
20
use MSlwk\Otomoto\Middleware\Webpage\Adapter\GETHandlerInterface;
21
22
/**
23
 * Class ReactPHPRequestAdapter
24
 * @package MSlwk\Otomoto\Middleware\Webpage\Adapter\ReactPH
25
 */
26
class ReactPHPRequestAdapter implements GETHandlerInterface
27
{
28
    /**
29
     * @var LoopInterface
30
     */
31
    private $loop;
32
33
    /**
34
     * @var ClientFactory
35
     */
36
    private $clientFactory;
37
38
    /**
39
     * ReactPHPRequestAdapter constructor.
40
     * @param LoopInterface $loop
41
     * @param ClientFactory $clientFactory
42
     */
43 5
    public function __construct(
44
        LoopInterface $loop,
45
        ClientFactory $clientFactory
46
    ) {
47 5
        $this->loop = $loop;
48 5
        $this->clientFactory = $clientFactory;
49 5
    }
50
51
    /**
52
     * @param UrlDTOArray $urlDTOArray
53
     * @return WebpageDTOArray
54
     */
55 4
    public function getWebpages(UrlDTOArray $urlDTOArray): WebpageDTOArray
56
    {
57 4
        $webpageDTOArray = new WebpageDTOArray();
58 4
        foreach ($urlDTOArray as $urlDTO) {
59 4
            $client = $this->clientFactory->create($this->loop);
60 4
            $request = $client->request('GET', $urlDTO->getUrl());
61
            $request->on('response', function (Response $response) use (&$webpageDTOArray) {
62 2
                $data = '';
63 2
                $response->on(
64 2
                    'data',
65
                    function ($chunk) use (&$webpageDTOArray, &$data) {
0 ignored issues
show
Unused Code introduced by
The import $webpageDTOArray is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
66 2
                        $data .= $chunk;
67 2
                    }
68 2
                )->on(
69 2
                    'end',
70
                    function () use (&$webpageDTOArray, &$data) {
71 2
                        $webpageDTOArray->add(new WebpageDTO($data));
72 2
                    }
73
                );
74 4
            });
75 4
            $request->end();
76
        }
77 4
        $this->loop->run();
78 4
        $this->validateWebpages($webpageDTOArray);
79 2
        return $webpageDTOArray;
80
    }
81
82
    /**
83
     * @param WebpageDTOArray $webpageDTOArray
84
     * @throws GETHandlerException
85
     */
86 4
    private function validateWebpages(WebpageDTOArray $webpageDTOArray)
87
    {
88 4
        if (!$webpageDTOArray->count()) {
89 2
            throw new GETHandlerException('An error occurred, no result found');
90
        }
91 2
    }
92
}
93