AbstractQueueHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.8333
cc 1
nc 1
nop 5
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\Factory;
20
use React\EventLoop\LoopInterface;
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
     * @param int|null $code
125
     * @param array|null $headers
126
     * @return void
127
     */
128
    protected function log(string $url, ?int $code = null, ?array $headers = []): void
129
    {
130
        $context = ['url' => $url, 'code' => $code, 'headers' => implode(',', $headers)];
131
132
        $this->logger->debug(
133
            "{$this->counter}/{$this->total}",
134
            $context
135
        );
136
    }
137
138
    /**
139
     * @return int
140
     */
141
    protected function getNumberOfParallelProcesses(): int
142
    {
143
        return min($this->getMaxNumberOfProcesses(), count($this->urls));
144
    }
145
146
    /**
147
     * @return void
148
     */
149
    protected function logProgress(): void
150
    {
151
        $this->queueProgressLogger->logProgress($this->getQueueProcessType(), $this->counter, $this->total);
152
    }
153
}
154