Passed
Pull Request — master (#875)
by Pauli
04:43 queued 02:11
created

PodcastUpdate::__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
nc 1
nop 3
dl 0
loc 6
rs 10
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 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
		;
44
	}
45
46
	protected function doExecute(InputInterface $input, OutputInterface $output, $users) {
47
		$olderThan = $input->getOption('older-than');
48
		if ($olderThan !== null) {
49
			$olderThan = (float)$olderThan;
50
		}
51
52
		if ($input->getOption('all')) {
53
			$this->userManager->callForAllUsers(function($user) use ($output, $olderThan) {
54
				$this->updateForUser($user->getUID(), $olderThan, $output);
55
			});
56
		} else {
57
			foreach ($users as $userId) {
58
				$this->updateForUser($userId, $olderThan, $output);
59
			}
60
		}
61
	}
62
63
	private function updateForUser(string $userId, ?float $olderThan, OutputInterface $output) : void {
64
		$output->writeln("Updating podcasts of <info>$userId</info>...");
65
66
		$result = $this->podcastService->updateAllChannels($userId, $olderThan, function (array $channelResult) use ($output) {
67
			if (isset($channelResult['channel'])) {
68
				$id = $channelResult['channel']->getId();
69
				$title = $channelResult['channel']->getTitle();
70
			} else {
71
				$id = -1;
72
				$title = '(unknown)';
73
			}
74
75
			if ($channelResult['updated']) {
76
				$output->writeln("  Channel $id <info>$title</info> was updated");
77
			} elseif ($channelResult['status'] === PodcastService::STATUS_OK) {
78
				$output->writeln("  Channel $id <info>$title</info> had no changes");
79
			} else {
80
				$output->writeln("  Channel $id <error>$title</error> update failed");
81
			}
82
		});
83
84
		if ($result['changed'] + $result['unchanged'] + $result['failed'] === 0) {
85
			$output->writeln("  (no channels to update)");
86
		}
87
	}
88
}
89