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

PodcastImport::doExecute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
nc 4
nop 3
dl 0
loc 14
rs 9.9666
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
use OCP\Files\NotFoundException;
19
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
use Symfony\Component\Console\Output\OutputInterface;
23
24
class PodcastImport extends BaseCommand {
25
26
	private IRootFolder $rootFolder;
27
	private PodcastService $podcastService;
28
29
	public function __construct(
30
			\OCP\IUserManager $userManager,
31
			\OCP\IGroupManager $groupManager,
32
			IRootFolder $rootFolder,
33
			PodcastService $podcastService) {
34
		$this->rootFolder = $rootFolder;
35
		$this->podcastService = $podcastService;
36
		parent::__construct($userManager, $groupManager);
37
	}
38
39
	protected function doConfigure() : void {
40
		$this
41
			->setName('music:podcast-import')
42
			->setDescription('import user podcast channels from an OPML file')
43
			->addOption(
44
				'file',
45
				null,
46
				InputOption::VALUE_REQUIRED,
47
				'path of the OPML file, relative to the user home folder'
48
			)
49
		;
50
	}
51
52
	protected function doExecute(InputInterface $input, OutputInterface $output, array $users) : void {
53
		$file = $input->getOption('file');
54
55
		if (empty($file)) {
56
			throw new \InvalidArgumentException('The OPML file path cannot be empty');
57
		}
58
59
		if ($input->getOption('all')) {
60
			$this->userManager->callForAllUsers(function($user) use ($output, $file) {
61
				$this->executeForUser($user->getUID(), $file, $output);
62
			});
63
		} else {
64
			foreach ($users as $userId) {
65
				$this->executeForUser($userId, $file, $output);
66
			}
67
		}
68
	}
69
70
	private function executeForUser(string $userId, string $filePath, OutputInterface $output) : void {
71
		$output->writeln("Importing podcast channels for <info>$userId</info>...");
72
73
		$userFolder = $this->rootFolder->getUserFolder($userId);
74
75
		try {
76
			$this->podcastService->importFromFile($userId, $userFolder, $filePath, function (array $channelResult) use ($output) {
77
				if (!empty($channelResult['channel'])) {
78
					$id = $channelResult['channel']->getId();
79
					$title = $channelResult['channel']->getTitle();
80
					$output->writeln("  Subscribed channel $id <info>$title</info>");
81
				} else if ($channelResult['status'] == PodcastService::STATUS_ALREADY_EXISTS) {
82
					$output->writeln("  Skipping already subscribed channel {$channelResult['rss']}");
83
				} else {
84
					$output->writeln("  Failed to subscribe <error>{$channelResult['rss']}</error>");
85
				}
86
			});
87
		} catch (NotFoundException $ex) {
88
			$output->writeln("  Invalid file path <error>$filePath</error>");
89
		} catch (\UnexpectedValueException $ex) {
90
			$output->writeln("  The file <error>$filePath</error> is not a supported OPML file");
91
		}
92
	}
93
94
}
95