Passed
Push — master ( e66d23...e16cc9 )
by Mariano
02:32 queued 01:03
created

PhiremockProcessManager   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 17
c 1
b 0
f 0
dl 0
loc 39
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A stop() 0 5 2
A start() 0 7 1
A __construct() 0 4 1
A initProcess() 0 8 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
    /** @var Process[] */
29
    private $processes;
30
31
    /** @var callable */
32
    private $output;
33
34
    public function __construct(callable $output)
35
    {
36
        $this->processes = [];
37
        $this->output = $output;
38
    }
39
40
    public function start(Config $config): void
41
    {
42
        $commandBuilder = new CommandBuilder($config);
43
        $process = $this->initProcess($commandBuilder);
44
        call_user_func($this->output, 'Running ' . $process->getCommandLine());
45
        $process->start();
46
        $this->processes[$process->getPid()] = $process;
47
    }
48
49
    public function stop(): void
50
    {
51
        foreach ($this->processes as $pid => $process) {
52
            call_user_func($this->output, "Stopping phiremock process with pid: " . $pid);
53
            $process->stop(3);
54
        }
55
    }
56
57
    private function initProcess(CommandBuilder $builder): Process
58
    {
59
        $commandline = $builder->build();
60
61
        if (method_exists(Process::class, 'fromShellCommandline')) {
62
            return Process::fromShellCommandline(implode(' ', $commandline));
63
        }
64
        return new Process(implode(' ', $commandline));
65
    }
66
}
67