Passed
Push — master ( 9ba67a...cb5537 )
by Mariano
02:06
created

PhiremockProcess::getCommandPrefix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
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->initProcess($ip, $port, $debug, $expectationsPath, $phiremockPath);
56
        $this->logPhiremockCommand($debug);
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
    }
62
63
    /**
64
     * Stops the process.
65
     */
66
    public function stop()
67
    {
68
        $this->process->stop(3);
69
    }
70
71
    /**
72
     * @param string $ip
73
     * @param int    $port
74
     * @param bool   $debug
75
     * @param string $expectationsPath
76
     * @param string $phiremockPath
77
     */
78
    private function initProcess($ip, $port, $debug, $expectationsPath, $phiremockPath)
79
    {
80
        $commandline = [
81
            $phiremockPath,
82
            '-i',
83
            $ip,
84
            '-p',
85
            $port
86
        ];
87
        if ($debug) {
88
            $commandline[] = '-d';
89
        }
90
        if ($expectationsPath) {
91
            $commandline[] = '-e';
92
            $commandline[] = $expectationsPath;
93
        }
94
95
        // Process wraps the command with 'exec' in UNIX OSs.
96
        $this->process = new Process($commandline);
97
    }
98
99
    /**
100
     * @param bool $debug
101
     */
102
    private function logPhiremockCommand($debug)
103
    {
104
        if ($debug) {
105
            echo 'Running ' . $this->process->getCommandLine() . PHP_EOL;
106
        }
107
    }
108
}
109