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

CommandBuilder::isWindows()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Mcustiel\Phiremock\Codeception\Extension;
4
5
class CommandBuilder
6
{
7
    private const LOG_FILE_NAME = 'phiremock.log';
8
9
    /** @var Config */
10
    private $config;
11
12
    public function __construct(Config $config)
13
    {
14
        $this->config = $config;
15
    }
16
17
    public function build(): array
18
    {
19
        $path = $this->config->getPhiremockPath();
20
        $phiremockPath = is_file($path) ? $path : $path . DIRECTORY_SEPARATOR . 'phiremock';
21
22
        $commandLine = [
23
            $this->getCommandPrefix() . $phiremockPath,
24
            '-i',
25
            $this->config->getInterface(),
26
            '-p',
27
            $this->config->getPort(),
28
        ];
29
30
        $this->addDebugMode($commandLine);
31
        $this->addExpectationsPath($commandLine);
32
        $this->addServerFactory($commandLine);
33
        $this->addCertificate($commandLine);
34
35
        $commandLine[] = '>';
36
        $this->addLogFile($commandLine);
37
        $commandLine[] = '2>&1';
38
39
        return $commandLine;
40
    }
41
42
    private function getCommandPrefix(): string
43
    {
44
        return $this->isWindows() ? '' : 'exec ';
45
    }
46
47
    private function isWindows(): bool
48
    {
49
        return DIRECTORY_SEPARATOR === '\\';
50
    }
51
52
    private function addLogFile(array &$commandline): void
53
    {
54
        $path = $this->config->getLogsPath();
55
        $logFile = is_dir($path) ? $path . DIRECTORY_SEPARATOR . self::LOG_FILE_NAME : $path;
56
        $commandline[] = $logFile;
57
    }
58
59
    private function addCertificate(array &$commandline): void
60
    {
61
        if ($this->config->getCertificatePath()) {
62
            $commandline[] = '-t';
63
            $commandline[] = $this->config->getCertificatePath();
64
            $commandline[] = '-k';
65
            $commandline[] = $this->config->getCertificateKeyPath();
66
            if ($this->config->getCertificatePassphrase()) {
67
                $commandline[] = '-s';
68
                $commandline[] = $this->config->getCertificatePassphrase();
69
            }
70
        }
71
    }
72
73
    private function addServerFactory(array &$commandline): void
74
    {
75
        if ($this->config->getServerFactory()) {
76
            $commandline[] = '-f';
77
            $commandline[] = escapeshellarg($this->config->getServerFactory());
78
        }
79
    }
80
81
    private function addExpectationsPath(array &$commandline): void
82
    {
83
        $path = $this->config->getExpectationsPath();
84
        $expectationsPath = is_dir($path) ? $path : '';
85
        if ($expectationsPath) {
86
            $commandline[] = '-e';
87
            $commandline[] = $expectationsPath;
88
        }
89
    }
90
91
    private function addDebugMode(array &$commandline): void
92
    {
93
        if ($this->config->isDebugMode()) {
94
            $commandline[] = '-d';
95
        }
96
    }
97
}
98