Passed
Push — master ( ca677b...6b166e )
by Mariano
07:37
created

PhiremockProcess   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 1
dl 0
loc 115
rs 10
c 0
b 0
f 0

8 Methods

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