Passed
Push — master ( 34942f...100000 )
by Bartosz
01:00 queued 11s
created

Model/QueueHandler/VarnishUrlRegenerator.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
    private function createRequest(string $url): void
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