Passed
Push — master ( 670257...8bfbef )
by Morris
11:30
created

Update::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018, michag86 ([email protected])
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OC\Core\Command\App;
23
24
use OCP\App\IAppManager;
25
use OC\Installer;
26
use OCP\ILogger;
27
use Symfony\Component\Console\Command\Command;
28
use Symfony\Component\Console\Input\InputArgument;
29
use Symfony\Component\Console\Input\InputOption;
30
use Symfony\Component\Console\Input\InputInterface;
31
use Symfony\Component\Console\Output\OutputInterface;
32
33
class Update extends Command {
34
35
	/** @var IAppManager */
36
	protected $manager;
37
	/** @var Installer */
38
	private $installer;
39
	/** @var ILogger */
40
	private $logger;
41
42
	/**
43
	 * @param IAppManager $manager
44
	 * @param Installer $installer
45
	 */
46
	public function __construct(IAppManager $manager, Installer $installer, ILogger $logger) {
47
		parent::__construct();
48
		$this->manager = $manager;
49
		$this->installer = $installer;
50
		$this->logger = $logger;
51
	}
52
53
	protected function configure() {
54
		$this
55
			->setName('app:update')
56
			->setDescription('update an app or all apps')
57
			->addArgument(
58
				'app-id',
59
				InputArgument::OPTIONAL,
60
				'update the specified app'
61
			)
62
			->addOption(
63
				'all',
64
				null,
65
				InputOption::VALUE_NONE,
66
				'update all updatable apps'
67
			)
68
			->addOption(
69
				'showonly',
70
				null,
71
				InputOption::VALUE_NONE,
72
				'show update(s) without updating'
73
			)
74
75
		;
76
	}
77
78
	protected function execute(InputInterface $input, OutputInterface $output) {
79
		$singleAppId = $input->getArgument('app-id');
80
81
		if ($singleAppId) {
82
			$apps = array($singleAppId);
83
			try {
84
				$this->manager->getAppPath($singleAppId);
0 ignored issues
show
Bug introduced by
It seems like $singleAppId can also be of type string[]; however, parameter $appId of OCP\App\IAppManager::getAppPath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

84
				$this->manager->getAppPath(/** @scrutinizer ignore-type */ $singleAppId);
Loading history...
85
			} catch (\OCP\App\AppPathNotFoundException $e) {
86
				$output->writeln($singleAppId . ' not installed');
0 ignored issues
show
Bug introduced by
Are you sure $singleAppId of type string|string[] can be used in concatenation? ( Ignorable by Annotation )

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

86
				$output->writeln(/** @scrutinizer ignore-type */ $singleAppId . ' not installed');
Loading history...
87
				return 1;
88
			}
89
90
		} else if ($input->getOption('all') || $input->getOption('showonly')) {
91
			$apps = \OC_App::getAllApps();
92
		} else {
93
			$output->writeln("<error>Please specify an app to update or \"--all\" to update all updatable apps\"</error>");
94
			return 1;
95
		}
96
97
		$return = 0;
98
		foreach ($apps as $appId) {
99
			$newVersion = $this->installer->isUpdateAvailable($appId);
100
			if ($newVersion) {
101
				$output->writeln($appId . ' new version available: ' . $newVersion);
102
103
				if (!$input->getOption('showonly')) {
104
					try {
105
						$result = $this->installer->updateAppstoreApp($appId);
106
					} catch(\Exception $e) {
107
						$this->logger->logException($e, ['message' => 'Failure during update of app "' . $appId . '"','app' => 'app:update']);
108
						$output->writeln('Error: ' . $e->getMessage());
109
						$return = 1;
110
					}
111
112
					if ($result === false) {
113
						$output->writeln($appId . ' couldn\'t be updated');
114
						$return = 1;
115
					} else if($result === true) {
116
						$output->writeln($appId . ' updated');
117
					}
118
				}
119
			}
120
		}
121
122
		return $return;
123
	}
124
}
125
126