HalfAsynchronousClient   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 81
Duplicated Lines 24.69 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 20
loc 81
c 0
b 0
f 0
wmc 5
lcom 1
cbo 9
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getContent() 0 16 3
A createRequestDefinition() 20 20 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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