Passed
Push — feature/786_podcasts ( f026ee )
by Pauli
11:46
created

PodcastAdd::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
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 14
rs 9.9666
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\BusinessLayer\PodcastChannelBusinessLayer;
16
use OCA\Music\BusinessLayer\PodcastEpisodeBusinessLayer;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Doctrine\Instantiator\Exception\InvalidArgumentException;
21
22
class PodcastAdd extends BaseCommand {
23
24
	/** @var PodcastChannelBusinessLayer */
25
	private $channelBusinessLayer;
26
	/** @var PodcastEpisodeBusinessLayer */
27
	private $episodeBusinessLayer;
28
29
	public function __construct(
30
			\OCP\IUserManager $userManager,
31
			\OCP\IGroupManager $groupManager,
32
			PodcastChannelBusinessLayer $channelBusinessLayer,
33
			PodcastEpisodeBusinessLayer $episodeBusinessLayer) {
34
		$this->channelBusinessLayer = $channelBusinessLayer;
35
		$this->episodeBusinessLayer = $episodeBusinessLayer;
36
		parent::__construct($userManager, $groupManager);
37
	}
38
39
	protected function doConfigure() {
40
		$this
41
			->setName('music:podcast-add')
42
			->setDescription('add a podcast channel from an RSS feed')
43
			->addOption(
44
					'rss',
45
					null,
46
					InputOption::VALUE_REQUIRED,
47
					'URL to an RSS feed'
48
			)
49
		;
50
	}
51
52
	protected function doExecute(InputInterface $input, OutputInterface $output, $users) {
53
		$rss = $input->getOption('rss');
54
55
		if (!$rss) {
56
			throw new \InvalidArgumentException("The named argument <error>rss</error> must be given!");
57
		}
58
59
		if ($input->getOption('all')) {
60
			$users = $this->userManager->callForAllUsers(function($user) use ($output, $rss) {
0 ignored issues
show
Unused Code introduced by
The assignment to $users is dead and can be removed.
Loading history...
61
				$this->addPodcast($user->getUID(), $rss, $output);
62
			});
63
		} else {
64
			foreach ($users as $userId) {
65
				$this->addPodcast($userId, $rss, $output);
66
			}
67
		}
68
	}
69
70
	private function addPodcast(string $userId, string $rss, OutputInterface $output) {
71
		$content = \file_get_contents($rss);
72
		if ($content === false) {
73
			throw new \InvalidArgumentException("Invalid URL <error>$rss</error>!");
74
		}
75
76
		$xmlTree = \simplexml_load_string($content, \SimpleXMLElement::class, LIBXML_NOCDATA);
77
		if ($xmlTree === false || !$xmlTree->channel) {
78
			throw new \InvalidArgumentException("The document at URL <error>$rss</error> is not a valid podcast RSS feed!");
79
		}
80
81
		$output->writeln("Adding podcast feed <info>$rss</info> for user <info>$userId</info>");
82
		try {
83
			$channel = $this->channelBusinessLayer->create($userId, $rss, $content, $xmlTree->channel);
84
85
			foreach ($xmlTree->channel->item as $episodeNode) {
86
				$this->episodeBusinessLayer->create($userId, $channel->getId(), $episodeNode);
0 ignored issues
show
Bug introduced by
The method create() does not exist on OCA\Music\BusinessLayer\...astEpisodeBusinessLayer. ( Ignorable by Annotation )

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

86
				$this->episodeBusinessLayer->/** @scrutinizer ignore-call */ 
87
                                 create($userId, $channel->getId(), $episodeNode);

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...
87
			}
88
		} catch (\OCA\Music\AppFramework\Db\UniqueConstraintViolationException $ex) {
89
			$output->writeln('User already has this podcast channel, skipping');
90
		}
91
	}
92
}
93