Passed
Pull Request — master (#8)
by Maciej
03:39
created

VarnishUrlPurger::createRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Importance

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