Test Failed
Push — master ( ce3067...29bb8f )
by Mariano
03:53
created

PhiremockProcess::start()   D

Complexity

Conditions 9
Paths 16

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 4.909
c 0
b 0
f 0
cc 9
eloc 19
nc 16
nop 6
1
<?php
2
/**
3
 * This file is part of codeception-phiremock-extension.
4
 *
5
 * phiremock-codeception-extension is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * phiremock-codeception-extension is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with phiremock-codeception-extension.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
namespace Codeception\Extension;
19
20
use Symfony\Component\Process\Process;
21
22
/**
23
 * Manages the current running Phiremock process.
24
 */
25
class PhiremockProcess
26
{
27
    /**
28
     * Phiremock server log.
29
     *
30
     * @var string
31
     */
32
    const LOG_FILE_NAME = 'phiremock.log';
33
34
    /**
35
     * @var \Symfony\Component\Process\Process
36
     */
37
    private $process;
38
39
    /**
40
     * Starts Phiremock.
41
     *
42
     * @param string $ip
43
     * @param int    $port
44
     * @param string $path
45
     * @param string $logsPath
46
     * @param bool   $debug
47
     */
48
    public function start($ip, $port, $path, $logsPath, $debug, $expectationsPath)
49
    {
50
        $phiremockPath = is_file($path) ? $path : $path . DIRECTORY_SEPARATOR . 'phiremock';
51
        $expectationsPath= is_dir($expectationsPath) ? $expectationsPath: '';
52
53
        if ($debug) {
54
            echo 'Running ' . $this->getCommandPrefix()
55
                . "{$phiremockPath} -i {$ip} -p {$port}"
56
                . ($debug? ' -d' : '')
57
                . ($expectationsPath? " -e {$expectationsPath}" : '' ) . PHP_EOL;
58
        }
59
        $this->process = new Process(
60
            $this->getCommandPrefix()
61
            . "{$phiremockPath} -i {$ip} -p {$port}"
62
            . ($debug ? ' -d' : '')
63
            . ($expectationsPath? " -e {$expectationsPath}" : '' )
64
        );
65
        $logFile = $logsPath . DIRECTORY_SEPARATOR . self::LOG_FILE_NAME;
66
        $this->process->start(function ($type, $buffer) use ($logFile) {
67
            file_put_contents($logFile, $buffer, FILE_APPEND);
68
        });
69
        $this->process->setEnhanceSigchildCompatibility(true);
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Proces...SigchildCompatibility() has been deprecated with message: since version 3.3, to be removed in 4.0.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
70
        if ($this->isWindows()) {
71
            $this->process->setEnhanceWindowsCompatibility(true);
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Proces...eWindowsCompatibility() has been deprecated with message: since version 3.3, to be removed in 4.0. Enhanced Windows compatibility will always be enabled.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
72
        }
73
    }
74
75
    /**
76
     * Stops the process.
77
     */
78
    public function stop()
79
    {
80
        if (!$this->isWindows()) {
81
            $this->process->signal(SIGTERM);
82
            $this->process->stop(3, SIGKILL);
83
        }
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    private function getCommandPrefix()
90
    {
91
        return $this->isWindows() ? '' : 'exec ';
92
    }
93
94
    /**
95
     * @return boolean
96
     */
97
    private function isWindows()
98
    {
99
        return PHP_OS == 'WIN32' || PHP_OS == 'WINNT' || PHP_OS == 'Windows';
100
    }
101
}
102