AsynchronousClient   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 75
Duplicated Lines 40 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 30
loc 75
c 0
b 0
f 0
wmc 4
lcom 1
cbo 9
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getContent() 10 10 2
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: 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
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...
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
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...
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