Completed
Push — master ( 59aad5...d2803a )
by Blizzz
16:43
created

Check   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 6 1
B execute() 0 32 7
1
<?php
2
3
/**
4
 *
5
 * @copyright Copyright (c) 2018, Tobia De Koninck ([email protected])
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\UpdateNotification\Command;
25
26
use OC\App\AppManager;
27
use OC\Installer;
28
use OCA\UpdateNotification\UpdateChecker;
29
use Symfony\Component\Console\Command\Command;
30
use Symfony\Component\Console\Input\InputInterface;
31
use Symfony\Component\Console\Output\OutputInterface;
32
33
class Check extends Command {
34
35
	/**
36
	 * @var Installer $installer
37
	 */
38
	private $installer;
39
40
	/**
41
	 * @var AppManager $appManager
42
	 */
43
	private $appManager;
44
45
	/**
46
	 * @var UpdateChecker $updateChecker
47
	 */
48
	private $updateChecker;
49
50
	public function __construct(AppManager $appManager, UpdateChecker $updateChecker, Installer $installer) {
51
		parent::__construct();
52
		$this->installer = $installer;
53
		$this->appManager = $appManager;
54
		$this->updateChecker = $updateChecker;
55
	}
56
57
	protected function configure() {
58
		$this
59
			->setName('update:check')
60
			->setDescription('Check for server and app updates')
61
		;
62
	}
63
64
	protected function execute(InputInterface $input, OutputInterface $output) {
65
		$updatesAvailableCount = 0;
66
67
		// Server
68
		$r = $this->updateChecker->getUpdateState();
69
		if (isset($r['updateAvailable']) && $r['updateAvailable']) {
70
			$output->writeln($r['updateVersion'] . ' is available. Get more information on how to update at '. $r['updateLink'] . '.');
71
			$updatesAvailableCount += 1;
72
		}
73
74
75
		// Apps
76
		$apps = $this->appManager->getInstalledApps();
77
		foreach ($apps as $app) {
78
			$update = $this->installer->isUpdateAvailable($app);
79
			if ($update !== false) {
80
				$output->writeln('Update for ' . $app . ' to version ' . $update . ' is available.');
81
				$updatesAvailableCount += 1;
82
			}
83
		}
84
85
		// Report summary
86
		if ($updatesAvailableCount === 0) {
87
			$output->writeln('<info>Everything up to date</info>');
88
		} else if ($updatesAvailableCount === 1) {
89
			$output->writeln('<comment>1 update available</comment>');
90
		} else {
91
			$output->writeln('<comment>' . $updatesAvailableCount . ' updates available</comment>');
92
		}
93
94
		return 0;
95
	}
96
}
97