Completed
Push — Parse-Tags ( be9c93...0426dd )
by Michael
04:21
created

UpdateCommand::getTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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