Passed
Push — feature/786_podcasts ( 3917cd...37128b )
by Pauli
02:11
created

PodcastUpdateCheck   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 44
rs 10
c 1
b 0
f 0
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 39 7
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\Backgroundjob;
14
15
use \OCA\Music\App\Music;
16
use \OCA\Music\Utility\PodcastService;
17
18
class PodcastUpdateCheck {
19
20
	/**
21
	 * Check podcast updates on the background
22
	 */
23
	public static function run() {
24
		$app = \OC::$server->query(Music::class);
25
26
		$container = $app->getContainer();
27
28
		$logger = $container->query('Logger');
29
		$logger->log('Run ' . \get_class(), 'debug');
30
31
		$minInterval = (float)$container->query('Config')->getSystemValue('music.podcast_auto_update_interval', 24); // hours
32
		// negative interval values can be used to disable the auto-update
33
		if ($minInterval >= 0) {
34
			$users = $container->query('PodcastChannelBusinessLayer')->findAllUsers();
35
			$podcastService = $container->query('PodcastService');
36
			$channelsChecked = 0;
37
38
			foreach ($users as $userId) {
39
				$podcastService->updateAllChannels($userId, $minInterval, function (array $channelResult) use ($logger, $userId, &$channelsChecked) {
40
					$id = (isset($channelResult['channel'])) ? $channelResult['channel']->getId() : -1;
41
42
					if ($channelResult['updated']) {
43
						$logger->log("Channel $id of user $userId was updated", 'debug');
44
					} elseif ($channelResult['status'] === PodcastService::STATUS_OK) {
45
						$logger->log("Channel $id of user $userId had no changes", 'debug');
46
					} else {
47
						$logger->log("Channel $id of user $userId update failed", 'debug');
48
					}
49
50
					$channelsChecked++;
51
				});
52
			}
53
54
			if ($channelsChecked === 0) {
55
				$logger->log('No podcast channels were due to check for updates', 'debug');
56
			} else {
57
				$logger->log("$channelsChecked podcast channels in total were checked for updates", 'debug');
58
			}
59
		}
60
		else {
61
			$logger->log('Automatic podcast updating is disabled via config.php', 'debug');
62
		}
63
64
	}
65
}
66