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

CommandBuilder   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 40
c 1
b 0
f 0
dl 0
loc 78
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A addExpectationsPath() 0 7 3
A addCertificate() 0 10 3
A addLogFile() 0 5 2
A build() 0 23 2
A __construct() 0 3 1
A addServerFactory() 0 5 2
A addDebugMode() 0 4 2
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,
0 ignored issues
show
Bug introduced by
The method getCommandPrefix() does not exist on Mcustiel\Phiremock\Codec...xtension\CommandBuilder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
            $this->/** @scrutinizer ignore-call */ 
22
                   getCommandPrefix() . $phiremockPath,

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $commandLine does not exist. Did you maybe mean $commandline?
Loading history...
38
    }
39
40
    private function addLogFile(array &$commandline): void
41
    {
42
        $path = $this->config->getLogsPath();
43
        $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...
44
        $commandline[] = $logFile;
45
    }
46
47
    private function addCertificate(array &$commandline): void
48
    {
49
        if ($this->config->getCertificatePath()) {
50
            $commandline[] = '-t';
51
            $commandline[] = $this->config->getCertificatePath();
52
            $commandline[] = '-k';
53
            $commandline[] = $this->config->getCertificateKeyPath();
54
            if ($this->config->getCertificatePassphrase()) {
55
                $commandline[] = '-s';
56
                $commandline[] = $this->config->getCertificatePassphrase();
57
            }
58
        }
59
    }
60
61
    private function addServerFactory(array &$commandline): void
62
    {
63
        if ($this->config->getServerFactory()) {
64
            $commandline[] = '-f';
65
            $commandline[] = escapeshellarg($this->config->getServerFactory());
66
        }
67
    }
68
69
    private function addExpectationsPath(array &$commandline): void
70
    {
71
        $path = $this->config->getExpectationsPath();
72
        $expectationsPath = is_dir($path) ? $path : '';
73
        if ($expectationsPath) {
74
            $commandline[] = '-e';
75
            $commandline[] = $expectationsPath;
76
        }
77
    }
78
79
    private function addDebugMode(array &$commandline): void
80
    {
81
        if ($this->config->isDebugMode()) {
82
            $commandline[] = '-d';
83
        }
84
    }
85
}
86