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

AbstractQueueHandler::runQueue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 4
nc 4
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * File: AbstractQueueHandler.php
7
 *
8
 * @author Maciej Sławik <[email protected]>
9
 * @author Bartosz Kubicki <[email protected]>
10
 * @copyright Copyright (C) 2019 Lizard Media (http://lizardmedia.pl)
11
 */
12
13
namespace LizardMedia\VarnishWarmer\Model\QueueHandler;
14
15
use LizardMedia\VarnishWarmer\Api\Config\GeneralConfigProviderInterface;
16
use LizardMedia\VarnishWarmer\Api\ProgressHandler\QueueProgressLoggerInterface;
17
use LizardMedia\VarnishWarmer\Model\Adapter\ReactPHP\ClientFactory;
18
use Psr\Log\LoggerInterface;
19
use React\EventLoop\LoopInterface;
20
use React\EventLoop\Factory;
21
22
/**
23
 * Class AbstractQueueHandler
24
 * @package LizardMedia\VarnishWarmer\Model\QueueHandler
25
 */
26
abstract class AbstractQueueHandler
27
{
28
    /**
29
     * @var GeneralConfigProviderInterface
30
     */
31
    protected $configProvider;
32
33
    /**
34
     * @var LoggerInterface
35
     */
36
    protected $logger;
37
38
    /**
39
     * @var QueueProgressLoggerInterface
40
     */
41
    protected $queueProgressLogger;
42
43
    /**
44
     * @var LoopInterface
45
     */
46
    protected $loop;
47
48
    /**
49
     * @var int
50
     */
51
    protected $counter = 0;
52
53
    /**
54
     * @var int
55
     */
56
    protected $total = 0;
57
58
    /**
59
     * @var array
60
     */
61
    protected $urls = [];
62
63
    /**
64
     * @var ClientFactory
65
     */
66
    protected $clientFactory;
67
68
    /**
69
     * AbstractQueueHandler constructor.
70
     * @param GeneralConfigProviderInterface $configProvider
71
     * @param LoggerInterface $logger
72
     * @param QueueProgressLoggerInterface $queueProgressLogger
73
     * @param Factory $loopFactory
74
     * @param ClientFactory $clientFactory
75
     */
76
    public function __construct(
77
        GeneralConfigProviderInterface $configProvider,
78
        LoggerInterface $logger,
79
        QueueProgressLoggerInterface $queueProgressLogger,
80
        Factory $loopFactory,
81
        ClientFactory $clientFactory
82
    ) {
83
        $this->configProvider = $configProvider;
84
        $this->logger = $logger;
85
        $this->queueProgressLogger = $queueProgressLogger;
86
        $this->loop = $loopFactory::create();
87
        $this->clientFactory = $clientFactory;
88
    }
89
90
    /**
91
     * @return int
92
     */
93
    abstract protected function getMaxNumberOfProcesses(): int;
94
95
    /**
96
     * @return string
97
     */
98
    abstract protected function getQueueProcessType(): string;
99
100
    /**
101
     * @param string $url
102
     * @return void
103
     */
104
    abstract protected function createRequest(string $url): void;
105
106
    /**
107
     * @return void
108
     */
109
    protected function runQueue(): void
110
    {
111
        while (!empty($this->urls)) {
112
            for ($i = 0; $i < $this->getMaxNumberOfProcesses(); $i++) {
113
                if (!empty($this->urls)) {
114
                    $this->createRequest(array_pop($this->urls));
115
                }
116
            }
117
118
            $this->loop->run();
119
        }
120
    }
121
122
    /**
123
     * @param string $url
124
     * @return void
125
     */
126
    protected function log(string $url): void
127
    {
128
        $this->logger->debug("{$this->counter}/{$this->total}", ['url' => $url]);
129
    }
130
131
    /**
132
     * @return int
133
     */
134
    protected function getNumberOfParallelProcesses(): int
135
    {
136
        return min($this->getMaxNumberOfProcesses(), count($this->urls));
137
    }
138
139
    /**
140
     * @return void
141
     */
142
    protected function logProgress(): void
143
    {
144
        $this->queueProgressLogger->logProgress($this->getQueueProcessType(), $this->counter, $this->total);
145
    }
146
}
147