Completed
Push — master ( 10f95b...bf15d4 )
by Michael
09:02
created

UpdateCommand::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
crap 2
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
 * @since          1.0
21
 */
22
class UpdateCommand extends AbstractController implements CommandInterface
23
{
24
	/**
25
	 * Execute the controller.
26
	 *
27
	 * @return  boolean
28
	 *
29
	 * @since   1.0
30
	 */
31
	public function execute()
32
	{
33
		$this->getApplication()->outputTitle('Update Server');
34
35
		$this->getApplication()->out('<info>Updating server to git HEAD</info>');
36
37
		// Pull from remote repo
38
		$this->runCommand('cd ' . APPROOT . ' && git pull 2>&1');
39
40
		$this->getApplication()->out('<info>Updating Composer resources</info>');
41
42
		// Run Composer install
43
		$this->runCommand('cd ' . APPROOT . ' && composer install --no-dev -o 2>&1');
44
45
		$this->getApplication()->out('<info>Update complete</info>');
46
47
		return true;
48
	}
49
50
	/**
51
	 * Get the command's description
52
	 *
53
	 * @return  string
54
	 *
55
	 * @since   1.0
56
	 */
57
	public function getDescription() : string
58
	{
59
		return 'Update the server to the current git HEAD.';
60
	}
61
62
	/**
63
	 * Get the command's title
64
	 *
65
	 * @return  string
66
	 *
67
	 * @since   1.0
68
	 */
69
	public function getTitle() : string
70
	{
71
		return 'Update Server';
72
	}
73
74
	/**
75
	 * Execute a command on the server.
76
	 *
77
	 * @param   string  $command  The command to execute.
78
	 *
79
	 * @return  string  Return data from the command
80
	 *
81
	 * @since   1.0
82
	 * @throws  \RuntimeException
83
	 */
84
	private function runCommand(string $command) : string
85
	{
86
		$lastLine = system($command, $status);
87
88
		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...
89
		{
90
			// Command exited with a status != 0
91
			if ($lastLine)
92
			{
93
				$this->getApplication()->out($lastLine);
94
95
				throw new \RuntimeException($lastLine);
96
			}
97
98
			$this->getApplication()->out('<error>An unknown error occurred</error>');
99
100
			throw new \RuntimeException('An unknown error occurred');
101
		}
102
103
		return $lastLine;
104
	}
105
}
106