Passed
Pull Request — master (#875)
by Pauli
11:55 queued 09:28
created

PodcastUpdate   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
c 1
b 0
f 0
dl 0
loc 73
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A updateForUser() 0 23 5
A doConfigure() 0 15 1
A doExecute() 0 14 4
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 2021
11
 */
12
13
namespace OCA\Music\Command;
14
15
use OCA\Music\Utility\PodcastService;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
class PodcastUpdate extends BaseCommand {
21
22
	/** @var PodcastService */
23
	private $podcastService;
24
25
	public function __construct(
26
			\OCP\IUserManager $userManager,
27
			\OCP\IGroupManager $groupManager,
28
			PodcastService $podcastService) {
29
		$this->podcastService = $podcastService;
30
		parent::__construct($userManager, $groupManager);
31
	}
32
33
	protected function doConfigure() {
34
		$this
35
			->setName('music:podcast-update')
36
			->setDescription('update podcast channels of one or more users from their sources')
37
			->addOption(
38
				'older-than',
39
				null,
40
				InputOption::VALUE_REQUIRED,
41
				'check updates only for channels which have not been checked for this many hours (sub-hour resolution supported with decimals)'
42
			)
43
			->addOption(
44
				'force',
45
				null,
46
				InputOption::VALUE_NONE,
47
				'update episodes even if there doesn\'t appear to be any changes'
48
			)
49
		;
50
	}
51
52
	protected function doExecute(InputInterface $input, OutputInterface $output, $users) {
53
		$olderThan = $input->getOption('older-than');
54
		if ($olderThan !== null) {
55
			$olderThan = (float)$olderThan;
56
		}
57
		$force = (bool)$input->getOption('force');
58
59
		if ($input->getOption('all')) {
60
			$this->userManager->callForAllUsers(function($user) use ($output, $olderThan, $force) {
61
				$this->updateForUser($user->getUID(), $olderThan, $force, $output);
62
			});
63
		} else {
64
			foreach ($users as $userId) {
65
				$this->updateForUser($userId, $olderThan, $force, $output);
66
			}
67
		}
68
	}
69
70
	private function updateForUser(string $userId, ?float $olderThan, bool $force, OutputInterface $output) : void {
71
		$output->writeln("Updating podcasts of <info>$userId</info>...");
72
73
		$result = $this->podcastService->updateAllChannels($userId, $olderThan, $force, function (array $channelResult) use ($output) {
74
			if (isset($channelResult['channel'])) {
75
				$id = $channelResult['channel']->getId();
76
				$title = $channelResult['channel']->getTitle();
77
			} else {
78
				$id = -1;
79
				$title = '(unknown)';
80
			}
81
82
			if ($channelResult['updated']) {
83
				$output->writeln("  Channel $id <info>$title</info> was updated");
84
			} elseif ($channelResult['status'] === PodcastService::STATUS_OK) {
85
				$output->writeln("  Channel $id <info>$title</info> had no changes");
86
			} else {
87
				$output->writeln("  Channel $id <error>$title</error> update failed");
88
			}
89
		});
90
91
		if ($result['changed'] + $result['unchanged'] + $result['failed'] === 0) {
92
			$output->writeln("  (no channels to update)");
93
		}
94
	}
95
}
96