Conditions | 8 |
Paths | 48 |
Total Lines | 73 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
27 | protected function execute(InputInterface $input, OutputInterface $output) |
||
28 | { |
||
29 | $io = new SymfonyStyle($input, $output); |
||
30 | |||
31 | $dryRun = (bool) $input->getOption('dry-run'); |
||
32 | |||
33 | if ($dryRun) { |
||
34 | $io->text('Dry run mode <info>enabled</info> (no images will be removed)'); |
||
35 | } |
||
36 | |||
37 | $downloadImages = $this->getContainer()->get('wallabag_core.entry.download_images'); |
||
38 | $baseFolder = $downloadImages->getBaseFolder(); |
||
39 | |||
40 | $io->text('Retrieve existing images'); |
||
41 | |||
42 | // retrieve _existing_ folders in the image folder |
||
43 | $finder = new Finder(); |
||
44 | $finder |
||
45 | ->directories() |
||
46 | ->ignoreDotFiles(true) |
||
47 | ->depth(2) |
||
48 | ->in($baseFolder); |
||
49 | |||
50 | $existingPaths = []; |
||
51 | foreach ($finder as $file) { |
||
52 | $existingPaths[] = $file->getFilename(); |
||
53 | } |
||
54 | |||
55 | $io->text(sprintf(' -> <info>%d</info> images found', \count($existingPaths))); |
||
56 | |||
57 | $io->text('Retrieve valid folders attached to a user'); |
||
58 | |||
59 | $entries = $this->getContainer()->get('wallabag_core.entry_repository')->findAllEntriesIdByUserId(); |
||
60 | |||
61 | // retrieve _valid_ folders from existing entries |
||
62 | $validPaths = []; |
||
63 | foreach ($entries as $entry) { |
||
64 | $path = $downloadImages->getRelativePath($entry['id']); |
||
65 | |||
66 | if (!file_exists($baseFolder . '/' . $path)) { |
||
67 | continue; |
||
68 | } |
||
69 | |||
70 | // only store the hash, not the full path |
||
71 | $validPaths[] = explode('/', $path)[2]; |
||
72 | } |
||
73 | |||
74 | $io->text(sprintf(' -> <info>%d</info> folders found', \count($validPaths))); |
||
75 | |||
76 | $deletedCount = 0; |
||
77 | |||
78 | $io->text('Remove images'); |
||
79 | |||
80 | // check if existing path are valid, if not, remove all images and the folder |
||
81 | foreach ($existingPaths as $existingPath) { |
||
82 | if (!\in_array($existingPath, $validPaths, true)) { |
||
83 | $fullPath = $baseFolder . '/' . $existingPath[0] . '/' . $existingPath[1] . '/' . $existingPath; |
||
84 | $files = glob($fullPath . '/*.*'); |
||
85 | |||
86 | if (!$dryRun) { |
||
87 | array_map('unlink', $files); |
||
|
|||
88 | rmdir($fullPath); |
||
89 | } |
||
90 | |||
91 | $deletedCount += \count($files); |
||
92 | |||
93 | $io->text(sprintf('Deleted images in <info>%s</info>: <info>%d</info>', $existingPath, \count($files))); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | $io->success(sprintf('Finished cleaning. %d deleted images', $deletedCount)); |
||
98 | |||
99 | return 0; |
||
100 | } |
||
102 |