Passed
Push — master ( 82dd60...c9370f )
by Maciej
02:44 queued 01:10
created

VarnishUrlPurger::buildQueue()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 22

Duplication

Lines 9
Ratio 40.91 %

Importance

Changes 0
Metric Value
dl 9
loc 22
c 0
b 0
f 0
rs 9.568
cc 2
nc 1
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A VarnishUrlPurger::getQueueProcessType() 0 4 1
1
<?php
2
/**
3
 * File: VarnishUrlPurger.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 Exception;
12
use LizardMedia\VarnishWarmer\Api\Config\GeneralConfigProviderInterface;
13
use LizardMedia\VarnishWarmer\Api\Config\PurgingConfigProviderInterface;
14
use LizardMedia\VarnishWarmer\Api\ProgressHandler\QueueProgressLoggerInterface;
15
use LizardMedia\VarnishWarmer\Api\QueueHandler\VarnishUrlPurgerInterface;
16
use LizardMedia\VarnishWarmer\Model\Adapter\ReactPHP\ClientFactory;
17
use Psr\Log\LoggerInterface;
18
use React\HttpClient\Response;
19
use React\EventLoop\Factory;
20
21
/**
22
 * Class VarnishUrlPurger
23
 * @package LizardMedia\VarnishWarmer\Model\QueueHandler
24
 */
25
class VarnishUrlPurger extends AbstractQueueHandler implements VarnishUrlPurgerInterface
26
{
27
    /**
28
     * @var string
29
     */
30
    const CURL_CUSTOMREQUEST = 'PURGE';
31
32
    /**
33
     * @var string
34
     */
35
    const PROCESS_TYPE = 'PURGE';
36
37
    /**
38
     * @var PurgingConfigProviderInterface
39
     */
40
    private $purgingConfigProvider;
41
42
    /**
43
     * VarnishUrlPurger constructor.
44
     * @param GeneralConfigProviderInterface $configProvider
45
     * @param LoggerInterface $logger
46
     * @param QueueProgressLoggerInterface $queueProgressLogger
47
     * @param Factory $loopFactory
48
     * @param ClientFactory $clientFactory
49
     * @param PurgingConfigProviderInterface $purgingConfigProvider
50
     */
51
    public function __construct(
52
        GeneralConfigProviderInterface $configProvider,
53
        LoggerInterface $logger,
54
        QueueProgressLoggerInterface $queueProgressLogger,
55
        Factory $loopFactory,
56
        ClientFactory $clientFactory,
57
        PurgingConfigProviderInterface $purgingConfigProvider
58
    ) {
59
        parent::__construct($configProvider, $logger, $queueProgressLogger, $loopFactory, $clientFactory);
60
        $this->purgingConfigProvider = $purgingConfigProvider;
61
    }
62
63
    /**
64
     * @param string $url
65
     * @return void
66
     */
67
    public function addUrlToPurge(string $url): void
68
    {
69
        $this->urls[] = $url;
70
        $this->total++;
71
    }
72
73
    /**
74
     * @return void
75
     */
76 View Code Duplication
    public function runPurgeQueue(): 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...
77
    {
78
        while (!empty($this->urls)) {
79
            for ($i = 0; $i < $this->getMaxNumberOfProcesses(); $i++) {
80
                if (!empty($this->urls)) {
81
                    $this->createRequest(array_pop($this->urls));
82
                }
83
            }
84
            $this->loop->run();
85
        }
86
    }
87
88
    /**
89
     * @return int
90
     */
91
    protected function getMaxNumberOfProcesses(): int
92
    {
93
        return $this->configProvider->getMaxConcurrentPurgeProcesses();
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    protected function getQueueProcessType(): string
100
    {
101
        return self::PROCESS_TYPE;
102
    }
103
104
    /**
105
     *
106
     * @param string $url
107
     * @return void
108
     */
109
    private function createRequest(string $url): void
110
    {
111
        $client = $this->clientFactory->create($this->loop);
112
        $request = $client->request('GET', $url, $this->buildHeaders());
113
        $request->on('response', function (Response $response) use ($url) {
114
            $response->on(
115
                'end',
116
                function () use ($url) {
117
                    $this->counter++;
118
                    $this->log($url);
119
                    $this->logProgress();
120
                }
121
            );
122
            $response->on('error', function (Exception $e) use ($url) {
123
                $this->logger->error(
124
                    $e->getMessage(),
125
                    [
126
                        'url' => $url
127
                    ]
128
                );
129
            });
130
        });
131
132
        $request->end();
133
    }
134
135
    /**
136
     * @return array
137
     */
138
    private function buildHeaders(): array
139
    {
140
        $headers = [];
141
        $headers[] = 'X-Magento-Tags-Pattern: .*';
142
        if ($this->purgingConfigProvider->isPurgeCustomHostEnabled()
143
            && $this->purgingConfigProvider->getAdditionalHostForHeader()) {
144
            $headers[] = "Host: {$this->purgingConfigProvider->getAdditionalHostForHeader()}";
145
        }
146
        return $headers;
147
    }
148
}
149