Passed
Push — master ( ecad09...a3aa81 )
by John
14:46 queued 12s
created

MigrateAdminConfig::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 3
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 4
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\NotFoundException;
31
use OCP\Files\SimpleFS\ISimpleFolder;
32
use OCP\IL10N;
33
use OCP\Migration\IOutput;
34
use OCP\Migration\IRepairStep;
35
use Throwable;
36
37
class MigrateAdminConfig implements IRepairStep {
38
	private IAppData $appData;
39
	private IL10N $l10n;
40
41
	public function __construct(
42
		IAppData $appData,
43
		IL10N $l10n
44
	) {
45
		$this->appData = $appData;
46
		$this->l10n = $l10n;
47
	}
48
49
	public function getName(): string {
50
		return $this->l10n->t('Failed to clean up the old admin theming images folder');
51
	}
52
53
	public function run(IOutput $output): void {
54
		$output->info('Migrating admin images');
55
		$this->migrateAdminImages($output);
56
		$this->cleanupAdminImages($output);
57
	}
58
59
	private function migrateAdminImages(IOutput $output): void {
60
		try {
61
			$images = $this->appData->getFolder('images');
62
			$output->info('Migrating admin images');
63
64
				// get or init the global folder if any
65
			try {
66
				$global = $this->appData->getFolder('global');
67
			} catch (NotFoundException $e) {
68
				$global = $this->appData->newFolder('global');
69
			}
70
71
			// get or init the new images folder if any
72
			try {
73
				$newImages = $global->getFolder('images');
74
			} catch (NotFoundException $e) {
75
				$newImages = $global->newFolder('images');
76
			}
77
			
78
			$files = $images->getDirectoryListing();
79
			$output->startProgress(count($files));
80
			foreach($files as $file) {
81
				$newImages->newFile($file->getName(), $file->getContent());
82
				$output->advance();
83
			}
84
85
			$output->finishProgress();
86
		} catch(NotFoundException $e) {
87
			$output->info('No admin images to migrate');
88
		}
89
	}
90
91
92
	private function cleanupAdminImages(IOutput $output): void {
93
		try {
94
			$images = $this->appData->getFolder('images');
95
			$images->delete();
96
		} catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
97
		} catch (Throwable $e) {
98
			$output->warning($this->l10n->t('Failed to cleanup the old admin image folder', [$e->getMessage()]));
99
		}
100
	}
101
}
102