Passed
Push — master ( 29bb8f...56a6a2 )
by Mariano
03:11
created

PhiremockProcess::start()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 4
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
19
namespace Codeception\Extension;
20
21
use Symfony\Component\Process\Process;
22
23
/**
24
 * Manages the current running Phiremock process.
25
 */
26
class PhiremockProcess
27
{
28
    /**
29
     * Phiremock server log.
30
     *
31
     * @var string
32
     */
33
    const LOG_FILE_NAME = 'phiremock.log';
34
35
    /**
36
     * @var \Symfony\Component\Process\Process
37
     */
38
    private $process;
39
40
    /**
41
     * Starts Phiremock.
42
     *
43
     * @param string $ip
44
     * @param int    $port
45
     * @param string $path
46
     * @param string $logsPath
47
     * @param bool   $debug
48
     * @param mixed  $expectationsPath
49
     */
50
    public function start($ip, $port, $path, $logsPath, $debug, $expectationsPath)
51
    {
52
        $phiremockPath = is_file($path) ? $path : $path . DIRECTORY_SEPARATOR . 'phiremock';
53
        $expectationsPath = is_dir($expectationsPath) ? $expectationsPath : '';
54
55
        $this->logPhiremockCommand($ip, $port, $debug, $expectationsPath, $phiremockPath);
56
        $this->initProcess($ip, $port, $debug, $expectationsPath, $phiremockPath);
57
        $logFile = $logsPath . DIRECTORY_SEPARATOR . self::LOG_FILE_NAME;
58
        $this->process->start(function ($type, $buffer) use ($logFile) {
59
            file_put_contents($logFile, $buffer, FILE_APPEND);
60
        });
61
        $this->setUpProcessCompatibility();
62
    }
63
64
    /**
65
     * Stops the process.
66
     */
67
    public function stop()
68
    {
69
        if (!$this->isWindows()) {
70
            $this->process->signal(SIGTERM);
71
            $this->process->stop(3, SIGKILL);
72
        }
73
    }
74
75
    private function setUpProcessCompatibility()
76
    {
77
        $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...
78
        if ($this->isWindows()) {
79
            $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...
80
        }
81
    }
82
83
    /**
84
     * @param string $ip
85
     * @param int    $port
86
     * @param bool   $debug
87
     * @param string $expectationsPath
88
     * @param string $phiremockPath
89
     */
90
    private function initProcess($ip, $port, $debug, $expectationsPath, $phiremockPath)
91
    {
92
        $this->process = new Process(
93
            $this->getCommandPrefix()
94
            . "{$phiremockPath} -i {$ip} -p {$port}"
95
            . ($debug ? ' -d' : '')
96
            . ($expectationsPath ? " -e {$expectationsPath}" : '')
97
        );
98
    }
99
100
    /**
101
     * @param string $ip
102
     * @param int    $port
103
     * @param bool   $debug
104
     * @param string $expectationsPath
105
     * @param string $phiremockPath
106
     */
107
    private function logPhiremockCommand($ip, $port, $debug, $expectationsPath, $phiremockPath)
108
    {
109
        if ($debug) {
110
            echo 'Running ' . $this->getCommandPrefix()
111
                . "{$phiremockPath} -i {$ip} -p {$port}"
112
                . ($debug ? ' -d' : '')
113
                . ($expectationsPath ? " -e {$expectationsPath}" : '') . PHP_EOL;
114
        }
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    private function getCommandPrefix()
121
    {
122
        return $this->isWindows() ? '' : 'exec ';
123
    }
124
125
    /**
126
     * @return bool
127
     */
128
    private function isWindows()
129
    {
130
        return PHP_OS === 'WIN32' || PHP_OS === 'WINNT' || PHP_OS === 'Windows';
131
    }
132
}
133