Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 59 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
CleanDataCommand | |
0.00% |
0 / 59 |
|
0.00% |
0 / 7 |
420 | |
0.00% |
0 / 1 |
getAllowedAppEnvs | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
handle | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
12 | |||
cleanThumbDir | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
30 | |||
forEachDirectoryEntry | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
42 | |||
opendir | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
readdir | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
closedir | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
mkdir | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
unlink | n/a |
0 / 0 |
n/a |
0 / 0 |
1 |
1 | <?php |
2 | |
3 | namespace Olz\Command; |
4 | |
5 | use Olz\Command\Common\OlzCommand; |
6 | use Olz\Utils\ImageUtils; |
7 | use Symfony\Component\Console\Attribute\AsCommand; |
8 | use Symfony\Component\Console\Command\Command; |
9 | use Symfony\Component\Console\Input\InputInterface; |
10 | use Symfony\Component\Console\Output\OutputInterface; |
11 | |
12 | #[AsCommand(name: 'olz:clean-data')] |
13 | class CleanDataCommand extends OlzCommand { |
14 | /** @return array<string> */ |
15 | protected function getAllowedAppEnvs(): array { |
16 | return ['dev', 'test', 'staging', 'prod']; |
17 | } |
18 | |
19 | protected function handle(InputInterface $input, OutputInterface $output): int { |
20 | $data_path = $this->envUtils()->getDataPath(); |
21 | $paths = array_values(ImageUtils::TABLES_IMG_DIRS); |
22 | sort($paths); |
23 | foreach ($paths as $path) { |
24 | $entities_path = "{$data_path}{$path}"; |
25 | $this->forEachDirectoryEntry( |
26 | $entities_path, |
27 | function ($entry) use ($entities_path) { |
28 | if (strval(intval($entry)) !== $entry) { |
29 | $this->logAndOutput("Invalid entity ID: {$entry}"); |
30 | return; |
31 | } |
32 | $entity_img_path = "{$entities_path}{$entry}/"; |
33 | $this->cleanThumbDir($entity_img_path); |
34 | } |
35 | ); |
36 | } |
37 | return Command::SUCCESS; |
38 | } |
39 | |
40 | private function cleanThumbDir(string $entity_img_path): void { |
41 | $thumb_dir = "{$entity_img_path}thumb/"; |
42 | try { |
43 | $this->forEachDirectoryEntry( |
44 | $thumb_dir, |
45 | function ($entry) use ($thumb_dir) { |
46 | $is_valid_thumb = preg_match('/^[a-zA-Z0-9_-]{24}\.jpg\$(32|64|128|256|512)\.jpg$/', $entry); |
47 | if (!$is_valid_thumb) { |
48 | $entry_path = "{$thumb_dir}{$entry}"; |
49 | $this->logAndOutput("Invalid thumb: {$entry_path}"); |
50 | $this->unlink($entry_path); |
51 | } |
52 | } |
53 | ); |
54 | } catch (\Throwable $th) { |
55 | $this->logAndOutput("Error validating thumbs: {$th->getMessage()}"); |
56 | } |
57 | |
58 | $img_dir = "{$entity_img_path}img/"; |
59 | $image_ids = []; |
60 | $this->forEachDirectoryEntry( |
61 | $img_dir, |
62 | function ($entry) use ($img_dir, &$image_ids) { |
63 | $is_valid_img = preg_match('/^[a-zA-Z0-9_-]{24}\.jpg$/', $entry); |
64 | if ($is_valid_img) { |
65 | $image_ids[] = $entry; |
66 | } else { |
67 | $entry_path = "{$img_dir}{$entry}"; |
68 | $this->logAndOutput("Invalid img: {$entry_path}"); |
69 | } |
70 | } |
71 | ); |
72 | try { |
73 | $this->imageUtils()->generateThumbnails($image_ids, $entity_img_path); |
74 | } catch (\Throwable $th) { |
75 | $this->logAndOutput("Error generating thumbnails: {$th->getMessage()}"); |
76 | } |
77 | } |
78 | |
79 | protected function forEachDirectoryEntry(string $directory, callable $callback): void { |
80 | if (!is_dir($directory)) { |
81 | $this->logAndOutput("Creating directory {$directory}..."); |
82 | $this->mkdir($directory, 0o777, true); |
83 | } |
84 | $handle = $this->opendir($directory); |
85 | if (!$handle) { |
86 | throw new \Exception("Failed to open directory {$directory}"); |
87 | } |
88 | while (false !== ($entry = $this->readdir($handle))) { |
89 | if ($entry === '.' || $entry === '..') { |
90 | continue; |
91 | } |
92 | $callback($entry); |
93 | } |
94 | $this->closedir($handle); |
95 | } |
96 | |
97 | /** @return false|resource */ |
98 | protected function opendir(string $path): mixed { |
99 | return @opendir($path); |
100 | } |
101 | |
102 | /** @param resource|null $handle */ |
103 | protected function readdir(mixed $handle): bool|string { |
104 | return @readdir($handle); |
105 | } |
106 | |
107 | /** @param resource|null $handle */ |
108 | protected function closedir(mixed $handle): void { |
109 | @closedir($handle); |
110 | } |
111 | |
112 | // @codeCoverageIgnoreStart |
113 | // Reason: Mocked in tests. |
114 | |
115 | protected function mkdir(string $directory, int $permissions, bool $recursive): void { |
116 | mkdir($directory, $permissions, $recursive); |
117 | } |
118 | |
119 | protected function unlink(string $path): void { |
120 | unlink($path); |
121 | } |
122 | |
123 | // @codeCoverageIgnoreEnd |
124 | } |