Passed
Push — feature/786_podcasts ( 5eb2d3...fb8d75 )
by Pauli
01:57
created

PodcastUpdate::updateForUser()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
nc 2
nop 3
dl 0
loc 19
rs 9.8333
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 ($channelResult) use ($output) {
67
			$channel = $channelResult['channel'] ?? null;
68
			$id = $channel->getId() ?? -1;
0 ignored issues
show
Bug introduced by
The method getId() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
			$id = $channel->/** @scrutinizer ignore-call */ getId() ?? -1;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
			$title = $channel->getTitle() ?? '(unknown)';
70
71
			if ($channelResult['updated']) {
72
				$output->writeln("  Channel $id <info>$title</info> was updated");
73
			} elseif ($channelResult['status'] === PodcastService::STATUS_OK) {
74
				$output->writeln("  Channel $id <info>$title</info> had no changes");
75
			} else {
76
				$output->writeln("  Channel $id <error>$title</error> update failed");
77
			}
78
		});
79
80
		if ($result['changed'] + $result['unchanged'] + $result['failed'] === 0) {
81
			$output->writeln("  (no channels to update)");
82
		}
83
	}
84
}
85