Passed
Pull Request — master (#40)
by Mariano
02:38
created

PhiremockProcessManager::getCommandPrefix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 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 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(Config $config): void
39
    {
40
        $process = $this->initProcess($config);
41
        $this->logPhiremockCommand($config->isDebugMode(), $process);
42
        $process->start();
43
        $this->processes[$process->getPid()] = $process;
44
    }
45
46
    public function stop(): void
47
    {
48
        foreach ($this->processes as $pid => $process) {
49
            echo "Stopping phiremock process with pid: " . $pid . PHP_EOL;
50
            $process->stop(3);
51
        }
52
    }
53
54
    private function initProcess(
55
        Config $config
56
    ): Process {
57
        $path = $config->getPhiremockPath();
58
        $phiremockPath = is_file($path) ? $path : $path . DIRECTORY_SEPARATOR . 'phiremock';
59
        $path = $config->getExpectationsPath();
60
        $expectationsPath = is_dir($path) ? $path : '';
61
        $path = $config->getLogsPath();
62
        $logFile = is_dir($path) ? $path . DIRECTORY_SEPARATOR . self::LOG_FILE_NAME : $path;
63
64
        $commandline = [
65
            $this->getCommandPrefix() . $phiremockPath,
66
            '-i',
67
            $config->getInterface(),
68
            '-p',
69
            $config->getPort(),
70
        ];
71
        if ($config->isDebugMode()) {
72
            $commandline[] = '-d';
73
        }
74
        if ($expectationsPath) {
75
            $commandline[] = '-e';
76
            $commandline[] = $expectationsPath;
77
        }
78
        if ($config->getServerFactory()) {
79
            $commandline[] = '-f';
80
            $commandline[] = escapeshellarg($config->getServerFactory());
81
        }
82
83
        if ($config->getCertificatePath()) {
84
            $commandline[] = '-t';
85
            $commandline[] = $config->getCertificatePath();
86
            $commandline[] = '-k';
87
            $commandline[] = $config->getCertificateKeyPath();
88
            if ($config->getCertificatePassphrase()) {
89
                $commandline[] = '-s';
90
                $commandline[] = $config->getCertificatePassphrase();
91
            }
92
        }
93
94
        $commandline[] = '>';
95
        $commandline[] = $logFile;
96
        $commandline[] = '2>&1';
97
98
        if (method_exists(Process::class, 'fromShellCommandline')) {
99
            return Process::fromShellCommandline(implode(' ', $commandline));
100
        }
101
        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

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