Passed
Pull Request — master (#8)
by Maciej
03:39
created

VarnishUrlRegenerator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 39.13 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 27
loc 69
c 0
b 0
f 0
wmc 8
lcom 1
cbo 7
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addUrlToRegenerate() 0 5 1
A runRegenerationQueue() 11 11 4
A getMaxNumberOfProcesses() 0 4 1
A getQueueProcessType() 0 4 1
A createRequest() 16 16 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
/**
3
 * File: VarnishUrlRegenerator.php
4
 *
5
 * @author Maciej Sławik <[email protected]>
6
 * @copyright Copyright (C) 2019 Lizard Media (http://lizardmedia.pl)
7
 */
8
9
namespace LizardMedia\VarnishWarmer\Model\QueueHandler;
10
11
use LizardMedia\VarnishWarmer\Api\QueueHandler\VarnishUrlRegeneratorInterface;
12
use React\HttpClient\Response;
13
14
/**
15
 * Class VarnishUrlRegenerator
16
 * @package LizardMedia\VarnishWarmer\Model\QueueHandler
17
 */
18
class VarnishUrlRegenerator extends AbstractQueueHandler implements VarnishUrlRegeneratorInterface
19
{
20
    /**
21
     * @var string
22
     */
23
    const PROCESS_TYPE = 'REGENERATE';
24
25
    /**
26
     * @param string $url
27
     * @return void
28
     */
29
    public function addUrlToRegenerate(string $url): void
30
    {
31
        $this->urls[] = $url;
32
        $this->total++;
33
    }
34
35
    /**
36
     * @return void
37
     */
38 View Code Duplication
    public function runRegenerationQueue(): 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...
39
    {
40
        while (!empty($this->urls)) {
41
            for($i = 0; $i < $this->getMaxNumberOfProcesses(); $i++) {
42
                if (!empty($this->urls)) {
43
                    $this->createRequest(array_pop($this->urls));
44
                }
45
            }
46
            $this->loop->run();
47
        }
48
    }
49
50
    /**
51
     * @return int
52
     */
53
    protected function getMaxNumberOfProcesses(): int
54
    {
55
        return $this->configProvider->getMaxConcurrentRegenerationProcesses();
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    protected function getQueueProcessType(): string
62
    {
63
        return self::PROCESS_TYPE;
64
    }
65
66
    /**
67
     * @param string $url
68
     * @return void
69
     */
70 View Code Duplication
    private function createRequest(string $url): 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...
71
    {
72
        $client = $this->clientFactory->create($this->loop);
73
        $request = $client->request('GET', $url);
74
        $request->on('response', function (Response $response) use ($url) {
75
            $response->on(
76
                'end',
77
                function () use ($url){
78
                    $this->counter++;
79
                    $this->log($url);
80
                    $this->logProgress();
81
                }
82
            );
83
        });
84
        $request->end();
85
    }
86
}
87