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

VarnishUrlRegenerator::runRegenerationQueue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
c 0
b 0
f 0
rs 9.9
cc 4
nc 4
nop 0

1 Method

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