Passed
Pull Request — master (#41)
by Mariano
01:34
created

PhiremockProcessManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 18
c 2
b 0
f 0
dl 0
loc 43
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A logPhiremockCommand() 0 4 2
A start() 0 7 1
A initProcess() 0 8 2
A stop() 0 5 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(Config $config): void
39
    {
40
        $commandBuilder = new CommandBuilder($config);
41
        $process = $this->initProcess($commandBuilder);
42
        $this->logPhiremockCommand($config->isDebugMode(), $process);
43
        $process->start();
44
        $this->processes[$process->getPid()] = $process;
45
    }
46
47
    public function stop(): void
48
    {
49
        foreach ($this->processes as $pid => $process) {
50
            echo "Stopping phiremock process with pid: " . $pid . PHP_EOL;
51
            $process->stop(3);
52
        }
53
    }
54
55
    private function initProcess(CommandBuilder $builder): Process
56
    {
57
        $commandline = $builder->build();
58
59
        if (method_exists(Process::class, 'fromShellCommandline')) {
60
            return Process::fromShellCommandline(implode(' ', $commandline));
61
        }
62
        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

62
        return new Process(/** @scrutinizer ignore-type */ implode(' ', $commandline));
Loading history...
63
    }
64
65
    private function logPhiremockCommand(bool $debug, Process $process): void
66
    {
67
        if ($debug) {
68
            echo 'Running ' . $process->getCommandLine() . PHP_EOL;
69
        }
70
    }
71
}
72