|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* File: FileQueueProgressLogger.php |
|
7
|
|
|
* |
|
8
|
|
|
* @author Maciej Sławik <[email protected]> |
|
9
|
|
|
* @copyright Copyright (C) 2019 Lizard Media (http://lizardmedia.pl) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace LizardMedia\VarnishWarmer\Model\ProgressHandler; |
|
13
|
|
|
|
|
14
|
|
|
use Exception; |
|
15
|
|
|
use LizardMedia\VarnishWarmer\Api\ProgressHandler\ProgressDataInterface; |
|
16
|
|
|
use LizardMedia\VarnishWarmer\Api\ProgressHandler\QueueProgressLoggerInterface; |
|
17
|
|
|
use Magento\Framework\App\Filesystem\DirectoryList; |
|
18
|
|
|
use Magento\Framework\Filesystem\Io\File; |
|
19
|
|
|
use Zend\Json\Json; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class FileQueueProgressLogger |
|
23
|
|
|
* @package LizardMedia\VarnishWarmer\Model\ProgressHandler |
|
24
|
|
|
*/ |
|
25
|
|
|
class FileQueueProgressLogger implements QueueProgressLoggerInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private const LOG_DIR = '/var/log/varnish/'; |
|
31
|
|
|
private const LOG_FILE = '.queue_progress.log'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var File |
|
35
|
|
|
*/ |
|
36
|
|
|
private $fileHandler; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
private $logDir; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
private $logFile; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var ProgressDataFactory |
|
50
|
|
|
*/ |
|
51
|
|
|
private $progressDataFactory; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* FileLock constructor. |
|
55
|
|
|
* @param DirectoryList $directoryList |
|
56
|
|
|
* @param File $fileHandler |
|
57
|
|
|
* @param ProgressDataFactory $progressDataFactory |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __construct( |
|
60
|
|
|
DirectoryList $directoryList, |
|
61
|
|
|
File $fileHandler, |
|
62
|
|
|
ProgressDataFactory $progressDataFactory |
|
63
|
|
|
) { |
|
64
|
|
|
$this->logDir = $directoryList->getRoot() . self::LOG_DIR; |
|
65
|
|
|
$this->logFile = $directoryList->getRoot() . self::LOG_DIR . self::LOG_FILE; |
|
66
|
|
|
$this->fileHandler = $fileHandler; |
|
67
|
|
|
$this->progressDataFactory = $progressDataFactory; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $type |
|
72
|
|
|
* @param int $current |
|
73
|
|
|
* @param int $total |
|
74
|
|
|
* @return void |
|
75
|
|
|
* @throws Exception |
|
76
|
|
|
*/ |
|
77
|
|
|
public function logProgress(string $type, int $current, int $total): void |
|
78
|
|
|
{ |
|
79
|
|
|
$this->fileHandler->checkAndCreateFolder($this->logDir); |
|
80
|
|
|
$this->fileHandler->write( |
|
81
|
|
|
$this->logFile, |
|
82
|
|
|
$this->prepareDataToLog($type, $current, $total) |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return ProgressDataInterface |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getProgressData(): ProgressDataInterface |
|
90
|
|
|
{ |
|
91
|
|
|
$loggedData = $this->fileHandler->read($this->logFile); |
|
92
|
|
|
return $this->retrieveLogData($loggedData); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param string $type |
|
97
|
|
|
* @param int $current |
|
98
|
|
|
* @param int $total |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
private function prepareDataToLog(string $type, int $current, int $total): string |
|
102
|
|
|
{ |
|
103
|
|
|
return Json::encode( |
|
104
|
|
|
[ |
|
105
|
|
|
ProgressDataInterface::FIELD_PROCESS_TYPE => $type, |
|
106
|
|
|
ProgressDataInterface::FIELD_CURRENT => $current, |
|
107
|
|
|
ProgressDataInterface::FIELD_TOTAL => $total |
|
108
|
|
|
] |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param string $loggedData |
|
114
|
|
|
* @return ProgressDataInterface |
|
115
|
|
|
*/ |
|
116
|
|
|
private function retrieveLogData(string $loggedData): ProgressDataInterface |
|
117
|
|
|
{ |
|
118
|
|
|
/** @var ProgressData $progressData */ |
|
119
|
|
|
$progressData = $this->progressDataFactory->create(); |
|
120
|
|
|
try { |
|
121
|
|
|
$loggedDataArray = Json::decode($loggedData, Json::TYPE_ARRAY); |
|
122
|
|
|
$progressData->addData($loggedDataArray); |
|
123
|
|
|
} catch (Exception $e) { |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $progressData; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|