Passed
Pull Request — master (#39)
by Mariano
02:50
created

PhiremockProcessManager::logPhiremockCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 2
nc 2
nop 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 Mcustiel\Phiremock\Codeception\Extension;
20
21
use Symfony\Component\Process\Process;
22
23
/**
24
 * Manages the current running Phiremock process.
25
 */
26
class PhiremockProcessManager
27
{
28
    const LOG_FILE_NAME = 'phiremock.log';
29
30
    /** @var \Symfony\Component\Process\Process[] */
31
    private $processes;
32
33
    public function __construct()
34
    {
35
        $this->processes = [];
36
    }
37
38
    public function start(
39
        string $ip,
40
        int $port,
41
        string $path,
42
        string $logsPath,
43
        bool $debug,
44
        ?string $expectationsPath,
45
        ?string $factoryClass
46
    ): void {
47
        $phiremockPath = is_file($path) ? $path : $path . DIRECTORY_SEPARATOR . 'phiremock';
48
        $expectationsPath = is_dir($expectationsPath) ? $expectationsPath : '';
49
        $logFile = $logsPath . DIRECTORY_SEPARATOR . self::LOG_FILE_NAME;
50
        $process = $this->initProcess($ip, $port, $debug, $expectationsPath, $phiremockPath, $logFile, $factoryClass);
51
        $this->logPhiremockCommand($debug, $process);
52
        $process->start();
53
        $this->processes[$process->getPid()] = $process;
54
    }
55
56
    public function stop(): void
57
    {
58
        foreach ($this->processes as $pid => $process) {
59
            echo "Stopping phiremock process with pid: " . $pid . PHP_EOL;
60
            $process->stop(3);
61
        }
62
    }
63
64
    private function initProcess(
65
        string $ip,
66
        int $port,
67
        bool $debug,
68
        ?string $expectationsPath,
69
        string $phiremockPath,
70
        string $logFile,
71
        ?string $factoryClass
72
    ): Process {
73
        $commandline = [
74
            $this->getCommandPrefix() . $phiremockPath,
75
            '-i',
76
            $ip,
77
            '-p',
78
            $port,
79
        ];
80
        if ($debug) {
81
            $commandline[] = '-d';
82
        }
83
        if ($expectationsPath) {
84
            $commandline[] = '-e';
85
            $commandline[] = $expectationsPath;
86
        }
87
        if ($factoryClass) {
88
            $commandline[] = '-f';
89
            $commandline[] = escapeshellarg($factoryClass);
90
        }
91
        $commandline[] = '>';
92
        $commandline[] = $logFile;
93
        $commandline[] = '2>&1';
94
95
        if (method_exists(Process::class, 'fromShellCommandline')) {
96
            return Process::fromShellCommandline(implode(' ', $commandline));
97
        }
98
        return new Process(implode(' ', $commandline));
1 ignored issue
show
Bug introduced by
implode(' ', $commandline) of type string is incompatible with the type array expected by parameter $command of Symfony\Component\Process\Process::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
        return new Process(/** @scrutinizer ignore-type */ implode(' ', $commandline));
Loading history...
99
    }
100
101
    private function logPhiremockCommand(bool $debug, Process $process): void
102
    {
103
        if ($debug) {
104
            echo 'Running ' . $process->getCommandLine() . PHP_EOL;
105
        }
106
    }
107
108
    private function getCommandPrefix(): string
109
    {
110
        return $this->isWindows() ? '' : 'exec ';
111
    }
112
113
    private function isWindows(): bool
114
    {
115
        return DIRECTORY_SEPARATOR === '\\';
116
    }
117
}
118