PackageInstaller::execute()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0582

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 11
cts 13
cp 0.8462
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 0
crap 4.0582
1
<?php
2
3
namespace Genesis\Commands\NodeJs;
4
5
use Genesis\Commands\Command;
6
use Genesis\Commands\Exec;
7
8
9
/**
10
 * @author Adam Bisek <[email protected]>
11
 */
12
class PackageInstaller extends Command
13
{
14
15
	private $directory;
16
17
	private $options;
18
19
20
	/**
21
	 * Gets working directory.
22
	 * @return string
23
	 */
24
	public function getDirectory()
25
	{
26
		return $this->directory;
27
	}
28
29
30
	/**
31
	 * Sets working directory. System will switch to this directory before running npm.
32
	 * @param string $directory
33
	 */
34 2
	public function setDirectory($directory)
35
	{
36 2
		$this->directory = $directory;
37 2
	}
38
39
40
	/**
41
	 * @return array|NULL
42
	 */
43 2
	public function getOptions()
44
	{
45 2
		return $this->options;
46
	}
47
48
49
	/**
50
	 * @param array|NULL $options
51
	 */
52 2
	public function setOptions(array $options = NULL)
53
	{
54 2
		$this->options = $options;
55 2
	}
56
57
58 2
	public function execute()
59
	{
60 2
		$cmd = 'cd ' . escapeshellarg($this->directory) . ' && npm install';
61 2
		if (isset($this->options['silent']) && $this->options['silent']) {
62 2
			$cmd .= ' --silent'; // supress warnings
63 2
		}
64 2
		$command = new Exec();
65 2
		$command->setCommand($cmd);
66 2
		$result = $command->execute();
67 2
		if ($result->getResult() !== 0) {
68
			$this->error(sprintf('Installation of node modules for package in dir "%s" failed.', $this->directory));
69
		}
70 2
		$this->log("Npm installed modules successfully.");
71 2
		return $result;
72
	}
73
74
}