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

VarnishUrlRegenerator::createRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
c 0
b 0
f 0
rs 9.7333
cc 1
nc 1
nop 1
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