Phantomjs   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 2
dl 0
loc 62
ccs 18
cts 21
cp 0.8571
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A kill() 0 4 1
A command() 0 19 4
A start() 0 13 4
1
<?php
2
3
namespace Openbuildings\Spiderling;
4
5
/**
6
 * A class for starting and stopping phantomjs service, using Spiderling assets
7
 *
8
 * @package    Openbuildings\Spiderling
9
 * @author     Ivan Kerin
10
 * @copyright  (c) 2013 OpenBuildings Ltd.
11
 * @license    http://spdx.org/licenses/BSD-3-Clause
12
 */
13
class Phantomjs {
14
15
	/**
16
	 * Start a phantomjs server in the background. Set port, server js file, additional files and log file.
17
	 *
18
	 * @param  string $file       the server js file
19
	 * @param  integer $port      the port to start the server on
20
	 * @param  string $additional additional file, passed to the js server
21
	 * @param  string $log_file
22
	 * @return string             the pid of the newly started process
23
	 */
24 3
	public static function start($file, $port, $additional = NULL, $log_file = '/dev/null')
25
	{
26 3
		if ( ! Network::is_port_open('localhost', $port))
27 1
			throw new Exception('Port :port is already taken', array(':port' => $port));
28
29 3
		if ($log_file !== '/dev/null' AND ! is_file($log_file))
30
			throw new Exception('Log file (:log_file) must be a file or /dev/null', array(':log_file' => $log_file));
31
32 3
		return shell_exec(strtr('nohup :command > :log 2> :log & echo $!', array(
33 3
			':command' => self::command($file, $port, $additional),
34 3
			':log' => $log_file,
35
		)));
36
	}
37
38
	/**
39
	 * kill a server on a given pid
40
	 * @param  string $pid
41
	 */
42 2
	public static function kill($pid)
43
	{
44 2
		shell_exec('kill '.$pid);
45 2
	}
46
47
	/**
48
	 * Return the command to start the phantomjs server
49
	 *
50
	 * @param  string $file       the server js file
51
	 * @param  integer $port
52
	 * @param  string $additional additional js file
53
	 * @return string
54
	 */
55 3
	public static function command($file, $port, $additional = NULL)
56
	{
57 3
		$dir = realpath(__DIR__.'/../../../assets').'/';
58
59 3
		$file = $dir.$file;
60
61 3
		if ( ! is_file($file))
62
			throw new Exception('Cannot start phantomjs: file :file is not found', array(':file' => $file));
63
64 3
		if ($additional)
0 ignored issues
show
Bug Best Practice introduced by
The expression $additional of type string|null is loosely compared to true; this is ambiguous if the string can be empty. 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 string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
65
		{
66 2
			if ( ! is_file($file))
67
				throw new Exception('Cannot start phantomjs: file :additional is not found', array(':additional' => $additional));
68
69 2
			$additional = $dir.$additional;
70
		}
71
72 3
		return "phantomjs --ssl-protocol=any --ignore-ssl-errors=true {$file} {$port} {$additional}";
73
	}
74
}
75