Completed
Push — master ( 8c6604...d079a5 )
by Sébastien
05:41
created

Config::getJavaBin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace PjbServer\Tools\StandaloneServer;
4
5
use PjbServer\Tools\Exception;
6
7
/*
8
  java -Djava.awt.headless="true"
9
  -Dphp.java.bridge.threads=50
10
  -Dphp.java.bridge.base=/usr/lib/php/modules
11
  -Dphp.java.bridge.php_exec=/usr/local/bin/php-cgi
12
  -Dphp.java.bridge.default_log_file=
13
  -Dphp.java.bridge.default_log_level=5
14
  -Dphp.java.bridge.daemon="false"
15
  -jar JavaBridge.jar
16
 * sudo netstat -anltp|grep :8089
17
 */
18
19
class Config
20
{
21
22
    /**
23
     * Default configuration options
24
     * @var array
25
     */
26
    protected $default_config = [
27
        'java_bin'   => 'java',
28
        'server_jar' => '{base_dir}/resources/pjb621_standalone/JavaBridge.jar',
29
        'log_file'   => '{base_dir}/var/pjbserver-port{tcp_port}.log',
30
        'pid_file'   => '{base_dir}/var/pjbserver-port{tcp_port}.pid',
31
    ];
32
33
    /**
34
     * Internal configuration array
35
     * @var array
36
     */
37
    protected $config;
38
39
40
    /**
41
     * Constructor
42
     *
43
     * <code>
44
     *
45
     * $params = [
46
     *      // required
47
     *      'port' => 8089,
48
     *
49
     *      // optionally
50
     *      'java_bin'   => 'java',
51
     *      'server_jar' => '{base_dir}/resources/pjb621_standalone/JavaBridge.jar',
52
     *      'log_file'   => '{base_dir}/var/pjbserver-port{tcp_port}.log',
53
     *      'java_bin'   => '{base_dir}/var/pjbserver-port{tcp_port}.pid',
54
     * ];
55
     * $config = new StandaloneServer\Config($params);
56
     * </code>
57
     *
58
     * @throws Exception\InvalidArgumentException
59
     * @param array $config
60
     * @param LoggerInterface $logger
0 ignored issues
show
Bug introduced by
There is no parameter named $logger. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
61
     *
62
     */
63 19
    public function __construct(array $config)
64
    {
65 19
        if (!isset($config['port'])) {
66 3
            throw new Exception\InvalidArgumentException("Error missing required 'port' in config");
67 16
        } elseif (!filter_var($config['port'], FILTER_VALIDATE_INT) || $config['port'] < 1) {
68 1
            throw new Exception\InvalidArgumentException("Option 'port' must be numeric greater than 0");
69
        }
70 15
        $config = array_merge($this->getDefaultConfig($config['port']), $config);
71 15
        $this->checkConfig($config);
72 14
        $this->config = $config;
73 14
    }
74
75
    /**
76
     * Return port on which standalone server listens
77
     * @return int
78
     */
79 11
    public function getPort()
80
    {
81 11
        return $this->config['port'];
82
    }
83
84
    /**
85
     * Return jar file of the server
86
     * @return string
87
     */
88 11
    public function getServerJar()
89
    {
90 11
        return $this->config['server_jar'];
91
    }
92
93
94
    /**
95
     * Return java binary
96
     * @return string
97
     */
98 11
    public function getJavaBin()
99
    {
100 11
        return $this->config['java_bin'];
101
    }
102
103
    /**
104
     * Return log file
105
     * @return string
106
     */
107 11
    public function getLogFile()
108
    {
109 11
        return $this->config['log_file'];
110
    }
111
112
    /**
113
     *
114
     * @return string
115
     */
116
    public function getClasspaths()
117
    {
118
        return $this->config['classpaths'];
119
    }
120
121
    /**
122
     * Return pid file where to store process id
123
     * @return string
124
     */
125 13
    public function getPidFile()
126
    {
127 13
        return $this->config['pid_file'];
128
    }
129
130
    /**
131
     * Return standalone configuration
132
     *
133
     * @return array
134
     */
135 1
    public function getConfig()
136
    {
137 1
        return $this->config;
138
    }
139
140
    /**
141
     * Return default configuration options
142
     * @param int $port
143
     * @return array
144
     */
145 15
    protected function getDefaultConfig($port)
146
    {
147 15
        $base_dir = realpath(__DIR__ . '/../../../../');
148 15
        $config = [];
149 15
        foreach ($this->default_config as $key => $value) {
150 15
            $tmp = str_replace('{base_dir}', $base_dir, $value);
151 15
            $tmp = str_replace('{tcp_port}', $port, $tmp);
152 15
            $config[$key] = $tmp;
153 15
        }
154 15
        return $config;
155
    }
156
157
    /**
158
     * Check configuration parameters
159
     * @throws Exception\InvalidArgumentException
160
     * @param array $config
161
     */
162 15
    protected function checkConfig(array $config)
163
    {
164
        // Step 1: all required options
165 15
        $required = ['port', 'server_jar', 'log_file', 'pid_file'];
166 15
        foreach ($required as $option) {
167 15
            if (!isset($config[$option]) || $config[$option] == '') {
168
                throw new Exception\InvalidArgumentException("Missing resuired configuration option: '$option''");
169
            }
170 15
        }
171
172
        // Step 2: server_jar file must exists
173 15
        if (!is_file($config['server_jar']) || !is_readable($config['server_jar'])) {
174 1
            throw new Exception\InvalidArgumentException("Server jar file not exists or unreadable. server-jar: '" . $config['server_jar'] ."'");
175
        }
176
177
        // Step 3: log and pid file should be creatable
178 14
        $temp_required_files = ['log_file', 'pid_file'];
179 14
        foreach ($temp_required_files as $option) {
180 14
            $file = $config[$option];
181 14
            $info = pathinfo($file);
182 14
            $dirname = $info['dirname'];
183 14
            if (!is_dir($dirname) || $dirname == ".") {
184
                $msg = "Option '$option' refer to an invalid or non-existent directory ($file)";
185
                throw new Exception\InvalidArgumentException($msg);
186
            }
187 14
            if (is_dir($file)) {
188
                $msg = "Option '$option' does not refer to a file but an existing directory ($file)";
189
                throw new Exception\InvalidArgumentException($msg);
190
            }
191 14
            if (file_exists($file) && !is_writable($file)) {
192
                $msg = "File specified in '$option' is not writable ($file)";
193
                throw new Exception\InvalidArgumentException($msg);
194
            }
195 14
        }
196
197
        // Step 4: Java must be callable
198
199
        // Step 5: Check autoload
200 14
    }
201
}
202