Completed
Pull Request — master (#215)
by Thomas
03:31
created

Application::assertOwnCloudFound()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 0
cts 17
cp 0
rs 8.8571
cc 6
eloc 13
nc 4
nop 0
crap 42
1
<?php
2
/**
3
 * @author Victor Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2015, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
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, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
namespace Owncloud\Updater\Console;
22
23
use Pimple\Container;
24
use Symfony\Component\Console\Output\StreamOutput;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Output\BufferedOutput;
27
use Symfony\Component\Console\Output\OutputInterface;
28
use Symfony\Component\Console\Command\Command;
29
use \Symfony\Component\Process\Exception\ProcessFailedException;
30
31
class Application extends \Symfony\Component\Console\Application {
32
33
	/** @var Container */
34
	public static $container;
35
36
37
	/** @var Container */
38
	protected $diContainer;
39
40
	/**
41
	 * Pass Pimple container into application
42
	 * @param Container $container
43
	 */
44
	public function setContainer(Container $container){
45
		$this->diContainer = $container;
46
		self::$container = $container;
47
	}
48
49
	/**
50
	 * Get Pimple container
51
	 * @return Container
52
	 */
53
	public function getContainer(){
54
		return $this->diContainer;
55
	}
56
57
	/**
58
	 * Get logger instance
59
	 * @return \Psr\Log\LoggerInterface
60
	 */
61
	public function getLogger(){
62
		return $this->diContainer['logger'];
63
	}
64
65
	/**
66
	 * Log exception with trace
67
	 * @param \Exception $e
68
	 */
69
	public function logException($e){
70
			$buffer = new BufferedOutput(OutputInterface::VERBOSITY_VERBOSE);
71
			$this->renderException($e, $buffer);
72
			$this->getLogger()->error($buffer->fetch());
73
	}
74
75
	public function doRun(InputInterface $input, OutputInterface $output){
76
		if (!($this->diContainer['logger.output'] instanceof StreamOutput)){
77
			$output->writeln('[Warning] Failed to init logger. Logging is disabled.');
78
		}
79
		try {
80
			// TODO: check if the current command needs a valid OC instance
81
			$this->assertOwnCloudFound();
82
			$this->initDirectoryStructure();
83
84
			$configReader = $this->diContainer['utils.configReader'];
85
			$commandName = $this->getCommandName($input);
86
			if (!in_array(
87
					$commandName,
88
					['upgrade:executeCoreUpgradeScripts', 'upgrade:checkpoint', 'upgrade:maintenanceMode', 'help', 'list']
89
				)
90
			){
91
				try {
92
					$configReader->init();
93
				} catch (ProcessFailedException $e){
94
					$this->logException($e);
95
					$output->writeln("<error>Initialization failed with message:</error>");
96
					$output->writeln($e->getProcess()->getOutput());
97
					$output->writeln('<info>Use upgrade:checkpoint --list to view a list of checkpoints</info>');
98
					$output->writeln('<info>upgrade:checkpoint --restore [checkpointid] to revert to the last checkpoint</info>');
99
					$output->writeln('Please attach your update.log to the issues you reporting.');
100
					return 1;
101
				}
102
				
103
			}
104
			return parent::doRun($input, $output);
105
		} catch (\Exception $e) {
106
			$this->logException($e);
107
			throw $e;
108
		}
109
	}
110
111
	protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output){
112
		if ($command instanceof \Owncloud\Updater\Command\Command){
113
			$command->setContainer($this->getContainer());
114
			$commandName = $this->getCommandName($input);
115
			$this->getLogger()->info('Execution of ' . $commandName . ' command started');
116
			if (!empty($command->getMessage())){
117
				$message = sprintf('<info>%s</info>', $command->getMessage());
118
				$output->writeln($message);
119
			}
120
			$exitCode = parent::doRunCommand($command, $input, $output);
121
			$this->getLogger()->info(
122
					'Execution of ' . $commandName . ' command stopped. Exit code is ' . $exitCode
123
			);
124
		} else {
125
			$exitCode = parent::doRunCommand($command, $input, $output);
126
		}
127
		return $exitCode;
128
	}
129
130
	/**
131
	 * Check for owncloud instance
132
	 * @throws \RuntimeException
133
	 */
134
	protected function assertOwnCloudFound(){
135
		$container = $this->getContainer();
136
		$locator = $container['utils.locator'];
137
		$pathToVersionFile = $locator->getPathToVersionFile();
138
		if (!file_exists($pathToVersionFile) || !is_file($pathToVersionFile)){
139
			throw new \RuntimeException('ownCloud is not found in ' . dirname($pathToVersionFile));
140
		}
141
142
		$pathToOccFile = $locator->getPathToOccFile();
143
		if (!file_exists($pathToOccFile) || !is_file($pathToOccFile)){
144
			throw new \RuntimeException('ownCloud is not found in ' . dirname($pathToOccFile));
145
		}
146
147
		// assert minimum version
148
		include $pathToVersionFile;
149
		$installedVersion = implode('.', $OC_Version);
0 ignored issues
show
Bug introduced by
The variable $OC_Version does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
150
		if (version_compare($installedVersion, '9.0.0', '<')) {
151
			throw new \RuntimeException("Minimum ownCloud version 9.0.0 is required for the updater - $installedVersion was found in " . dirname($pathToVersionFile));
152
		}
153
	}
154
155
	/**
156
	 * Create proper directory structure to store data
157
	 */
158
	protected function initDirectoryStructure(){
159
		$container = $this->getContainer();
160
		$locator = $container['utils.locator'];
161
		$fsHelper = $container['utils.filesystemhelper'];
162
		if (!file_exists($locator->getDataDir())){
163
			$fsHelper->mkdir($locator->getDataDir());
164
		}
165
		if (!file_exists($locator->getDownloadBaseDir())){
166
			$fsHelper->mkdir($locator->getDownloadBaseDir());
167
		}
168
		if (!file_exists($locator->getCheckpointDir())){
169
			$fsHelper->mkdir($locator->getCheckpointDir());
170
		}
171
	}
172
173
}
174