Completed
Push — master ( f913a0...098aab )
by Sébastien
02:29
created

StandaloneServer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
namespace PjbServer\Tools;
3
4
class StandaloneServer
5
{
6
    /**
7
     * @var int
8
     */
9
    protected $port;
10
11
    /**
12
     *
13
     * @var array
14
     */
15
    protected $config;
16
17
18
    /**
19
     * Tells whether the standalone server is started
20
     * @var boolean
21
     */
22
    protected $started = false;
23
24
    /**
25
     * @var array
26
     */
27
    protected $required_arguments = array(
28
        'server_port' => FILTER_VALIDATE_INT,
29
    );
30
31
    /**
32
     * Constructor
33
     *
34
     * @throws Exception\InvalidArgumentException
35
     * @param array $config
36
     */
37 4
    public function __construct(array $config)
38
    {
39 4
        $this->checkConfig($config);
40 2
        $this->setServerPort($config['server_port']);
41
42 2
    }
43
44
    /**
45
     * Check configuration parameters
46
     * @throws Exception\InvalidArgumentException
47
     * @param array $config
48
     */
49 4
    protected function checkConfig(array $config)
50
    {
51 4
        foreach ($this->required_arguments as $name => $type) {
52 4
            if (!isset($config[$name])) {
53 1
                $msg = "Missing option '$name' in Standalone server configuration";
54 1
                throw new Exception\InvalidArgumentException($msg);
55
            }
56
57 3
            if ($type !== null && !filter_var($config[$name], $type)) {
58 1
                $msg = "Unsupported type in option '$name' in Standalone server configuration (required $type)";
59 1
                throw new Exception\InvalidArgumentException($msg);
60
            }
61 2
        }
62 2
    }
63
64
65
    /**
66
     * Start the standalone server
67
     * @return void
68
     */
69 1
    public function start()
70
    {
71 1
        $port = $this->getServerPort();
0 ignored issues
show
Unused Code introduced by
$port is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
72 1
        $this->started = true;
73
74
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
75
        $command = "java -cp $jar_dir/mysql-connector-java-5.1.36-bin.jar:$jar_file php.java.bridge.Standalone SERVLET:$port";
76
        $error_file = "$test_dir/logs/pjb-standalone-error.log";
77
        $pid_file   = "$test_dir/logs/pjb-standalone.pid";
78
79
        if (!self::isStandaloneServerRunning($pid_file)) {
80
            echo "\nStarting standalone pjb server:\n $command\n";
81
            echo "@see logs in     : $error_file\n";
82
            echo "@see pid file in : $pid_file\n";
83
84
            $cmd = sprintf("%s > %s 2>&1 & echo $! > %s", $command, $error_file, $pid_file);
85
            exec($cmd);
86
87
            // let time for server to start
88
            if (preg_match('/travis/', dirname(__FILE__))) {
89
                sleep(8);
90
            } else {
91
                sleep(1);
92
            }
93
        } else {
94
            echo "Standalone server already running, skipping start\n";
95
        }
96
97
        register_shutdown_function(array(__CLASS__, 'killStandaloneServer'));
98
        */
99 1
    }
100
101
102 1
    public function stop()
103
    {
104 1
        $this->started = false;
105 1
    }
106
107
    /**
108
     * Tells whether the standalone server is started
109
     * @return boolean
110
     */
111 1
    public function isStarted()
112
    {
113 1
        return $this->started;
114
    }
115
116
    /**
117
     * Restart the standalone server
118
     */
119
    public function restart()
120
    {
121
        $this->stop();
122
        $this->start();
123
    }
124
125
    /**
126
     * Return port on which standalone server listens
127
     * @return int
128
     */
129 2
    public function getServerPort()
130
    {
131 2
        return $this->port;
132
    }
133
    
134
    /**
135
     * Set port on which standalone server listens.
136
     *
137
     * @param int $port
138
     * @return void
139
     */
140 2
    protected function setServerPort($port)
141
    {
142 2
        $this->port = $port;
143 2
    }
144
    
145
}
0 ignored issues
show
Coding Style introduced by
According to PSR2, the closing brace of classes should be placed on the next line directly after the body.

Below you find some examples:

// Incorrect placement according to PSR2
class MyClass
{
    public function foo()
    {

    }
    // This blank line is not allowed.

}

// Correct
class MyClass
{
    public function foo()
    {

    } // No blank lines after this line.
}
Loading history...
146