Passed
Push — master ( b832d4...42a9c0 )
by Blizzz
34:32 queued 18:42
created

CleanupOldCache::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2022 Christopher Ng <[email protected]>
7
 *
8
 * @author Christopher Ng <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Theming\Migration;
28
29
use OCP\Files\IAppData;
30
use OCP\Files\SimpleFS\ISimpleFolder;
31
use OCP\IL10N;
32
use OCP\Migration\IOutput;
33
use OCP\Migration\IRepairStep;
34
use Throwable;
35
36
class CleanupOldCache implements IRepairStep {
37
	private const CACHE_FOLDERS = [
38
		'global',
39
		'users',
40
	];
41
42
	private IAppData $appData;
43
	private IL10N $l10n;
44
45
	public function __construct(
46
		IAppData $appData,
47
		IL10N $l10n
48
	) {
49
		$this->appData = $appData;
50
		$this->l10n = $l10n;
51
	}
52
53
	public function getName(): string {
54
		return $this->l10n->t('Cleanup old theming cache');
55
	}
56
57
	public function run(IOutput $output): void {
58
		$folders = array_filter(
59
			$this->appData->getDirectoryListing(),
60
			fn (ISimpleFolder $folder): bool => !in_array($folder->getName(), static::CACHE_FOLDERS, true),
61
		);
62
63
		$output->startProgress(count($folders));
64
65
		foreach ($folders as $folder) {
66
			try {
67
				$folder->delete();
68
			} catch (Throwable $e) {
69
				$output->warning($this->l10n->t('Failed to delete folder: "%1$s", error: %2$s', [$folder->getName(), $e->getMessage()]));
70
			}
71
			$output->advance();
72
		}
73
74
		$output->finishProgress();
75
	}
76
}
77