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

CommandBuilder::addCertificate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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