Passed
Push — master ( 124be7...f358a5 )
by Pauli
03:35 queued 16s
created

PodcastExport::doConfigure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 16
rs 9.7998
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2025
11
 */
12
13
namespace OCA\Music\Command;
14
15
use OCA\Music\Utility\PodcastService;
16
17
use OCP\Files\IRootFolder;
18
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
class PodcastExport extends BaseCommand {
24
25
	private IRootFolder $rootFolder;
26
	private PodcastService $podcastService;
27
28
	public function __construct(
29
			\OCP\IUserManager $userManager,
30
			\OCP\IGroupManager $groupManager,
31
			IRootFolder $rootFolder,
32
			PodcastService $podcastService) {
33
		$this->rootFolder = $rootFolder;
34
		$this->podcastService = $podcastService;
35
		parent::__construct($userManager, $groupManager);
36
	}
37
38
	protected function doConfigure() : void {
39
		$this
40
			->setName('music:podcast-export')
41
			->setDescription('export user podcast channels to an OPML file')
42
			->addOption(
43
				'file',
44
				null,
45
				InputOption::VALUE_REQUIRED,
46
				'target file path, relative to the user home folder',
47
				'Podcasts.opml'
48
			)
49
			->addOption(
50
				'overwrite',
51
				null,
52
				InputOption::VALUE_NONE,
53
				'overwrite the target file if it already exists'
54
			)
55
		;
56
	}
57
58
	protected function doExecute(InputInterface $input, OutputInterface $output, array $users) : void {
59
		$path = $input->getOption('file');
60
61
		list('basename' => $file, 'dirname' => $dir) = \pathinfo($path);
62
63
		$overwrite = (bool)$input->getOption('overwrite');
64
65
		if (empty($file)) {
66
			throw new \InvalidArgumentException('Invalid argument: The file name must not be empty');
67
		}
68
69
		if ($input->getOption('all')) {
70
			$this->userManager->callForAllUsers(function($user) use ($output, $dir, $file, $overwrite) {
71
				$this->executeForUser($user->getUID(), $dir, $file, $overwrite, $output);
72
			});
73
		} else {
74
			foreach ($users as $userId) {
75
				$this->executeForUser($userId, $dir, $file, $overwrite, $output);
76
			}
77
		}
78
	}
79
80
	private function executeForUser(string $userId, string $dir, string $fileName, bool $overwrite, OutputInterface $output) : void {
81
		$output->writeln("Exporting podcast channels of <info>$userId</info>...");
82
83
		$userFolder = $this->rootFolder->getUserFolder($userId);
84
85
		try {
86
			$filePath = $this->podcastService->exportToFile($userId, $userFolder, $dir, $fileName, $overwrite ? 'overwrite' : 'abort');
87
			$output->writeln("  Exported to <info>$filePath</info>");
88
		} catch (\OCP\Files\NotFoundException $ex) {
89
			$output->writeln("  Invalid folder path <error>$dir</error>");
90
		} catch (\RuntimeException $ex) {
91
			$output->writeln("  File in the path <error>$dir/$fileName</error> already exists, pass the argument <info>--overwrite</info> to overwrite it or specify different path with the argument <info>--file</info>");
92
		}
93
	}
94
95
}
96