CommandBuilder::addExpectationsPath()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 3
nc 4
nop 1
1
<?php
2
3
/**
4
 * This file is part of phiremock-codeception-extension.
5
 *
6
 * phiremock-codeception-extension is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * phiremock-codeception-extension is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with phiremock-codeception-extension.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace Mcustiel\Phiremock\Codeception\Extension;
21
22
class CommandBuilder
23
{
24
    private const LOG_FILE_NAME = 'phiremock.log';
25
26
    /** @var Config */
27
    private $config;
28
29
    public function __construct(Config $config)
30
    {
31
        $this->config = $config;
32
    }
33
34
    public function build(): array
35
    {
36
        $path = $this->config->getPhiremockPath();
37
        $phiremockPath = is_file($path) ? $path : $path . DIRECTORY_SEPARATOR . 'phiremock';
38
39
        $commandLine = [
40
            $this->getCommandPrefix() . $phiremockPath,
41
            '-i',
42
            $this->config->getInterface(),
43
            '-p',
44
            $this->config->getPort(),
45
        ];
46
47
        $this->addDebugMode($commandLine);
48
        $this->addExpectationsPath($commandLine);
49
        $this->addServerFactory($commandLine);
50
        $this->addCertificate($commandLine);
51
52
        $commandLine[] = '>';
53
        $this->addLogFile($commandLine);
54
        $commandLine[] = '2>&1';
55
56
        return $commandLine;
57
    }
58
59
    private function getCommandPrefix(): string
60
    {
61
        return $this->isWindows() ? '' : 'exec ';
62
    }
63
64
    private function isWindows(): bool
65
    {
66
        return DIRECTORY_SEPARATOR === '\\';
67
    }
68
69
    private function addLogFile(array &$commandline): void
70
    {
71
        $path = $this->config->getLogsPath();
72
        $logFile = is_dir($path) ? $path . DIRECTORY_SEPARATOR . self::LOG_FILE_NAME : $path;
73
        $commandline[] = $logFile;
74
    }
75
76
    private function addCertificate(array &$commandline): void
77
    {
78
        if ($this->config->getCertificatePath()) {
79
            $commandline[] = '-t';
80
            $commandline[] = $this->config->getCertificatePath();
81
            $commandline[] = '-k';
82
            $commandline[] = $this->config->getCertificateKeyPath();
83
            if ($this->config->getCertificatePassphrase()) {
84
                $commandline[] = '-s';
85
                $commandline[] = $this->config->getCertificatePassphrase();
86
            }
87
        }
88
    }
89
90
    private function addServerFactory(array &$commandline): void
91
    {
92
        if ($this->config->getServerFactory()) {
93
            $commandline[] = '-f';
94
            $commandline[] = escapeshellarg($this->config->getServerFactory());
95
        }
96
    }
97
98
    private function addExpectationsPath(array &$commandline): void
99
    {
100
        $path = $this->config->getExpectationsPath();
101
        $expectationsPath = is_dir($path) ? $path : '';
102
        if ($expectationsPath) {
103
            $commandline[] = '-e';
104
            $commandline[] = $expectationsPath;
105
        }
106
    }
107
108
    private function addDebugMode(array &$commandline): void
109
    {
110
        if ($this->config->isDebugMode()) {
111
            $commandline[] = '-d';
112
        }
113
    }
114
}
115