Completed
Push — mysql_improvements ( 2e95ce...dedbef )
by Michael
03:52
created

UpdateCommand::runCommand()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 9
cp 0
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
crap 12
1
<?php
2
/**
3
 * Joomla! Statistics Server
4
 *
5
 * @copyright  Copyright (C) 2013 - 2017 Open Source Matters, Inc. All rights reserved.
6
 * @license    http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later
7
 */
8
9
namespace Joomla\StatsServer\Commands;
10
11
use Joomla\Controller\AbstractController;
12
use Joomla\StatsServer\CommandInterface;
13
14
/**
15
 * Update command
16
 *
17
 * @method         \Joomla\StatsServer\CliApplication  getApplication()  Get the application object.
18
 * @property-read  \Joomla\StatsServer\CliApplication  $app              Application object
19
 */
20
class UpdateCommand extends AbstractController implements CommandInterface
21
{
22
	/**
23
	 * Execute the controller.
24
	 *
25
	 * @return  boolean
26
	 */
27
	public function execute()
28
	{
29
		$this->getApplication()->outputTitle('Update Server');
30
31
		$this->getApplication()->out('<info>Updating server to git HEAD</info>');
32
33
		// Pull from remote repo
34
		$this->runCommand('cd ' . APPROOT . ' && git pull 2>&1');
35
36
		$this->getApplication()->out('<info>Updating Composer resources</info>');
37
38
		// Run Composer install
39
		$this->runCommand('cd ' . APPROOT . ' && composer install --no-dev -o 2>&1');
40
41
		$this->getApplication()->out('<info>Update complete</info>');
42
43
		return true;
44
	}
45
46
	/**
47
	 * Get the command's description
48
	 *
49
	 * @return  string
50
	 */
51
	public function getDescription() : string
52
	{
53
		return 'Update the server to the current git HEAD.';
54
	}
55
56
	/**
57
	 * Get the command's title
58
	 *
59
	 * @return  string
60
	 */
61
	public function getTitle() : string
62
	{
63
		return 'Update Server';
64
	}
65
66
	/**
67
	 * Execute a command on the server.
68
	 *
69
	 * @param   string  $command  The command to execute.
70
	 *
71
	 * @return  string  Return data from the command
72
	 *
73
	 * @throws  \RuntimeException
74
	 */
75
	private function runCommand(string $command) : string
76
	{
77
		$lastLine = system($command, $status);
78
79
		if ($status)
0 ignored issues
show
Bug Best Practice introduced by
The expression $status of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
80
		{
81
			// Command exited with a status != 0
82
			if ($lastLine)
83
			{
84
				$this->getApplication()->out($lastLine);
85
86
				throw new \RuntimeException($lastLine);
87
			}
88
89
			$this->getApplication()->out('<error>An unknown error occurred</error>');
90
91
			throw new \RuntimeException('An unknown error occurred');
92
		}
93
94
		return $lastLine;
95
	}
96
}
97