Passed
Push — master ( 90d55d...0e16d5 )
by Carlos C
03:42 queued 10s
created

Caller::createShellExecTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace CfdiUtils\OpenSSL;
3
4
use CfdiUtils\Utils\Internal\ShellExec;
5
use CfdiUtils\Utils\Internal\ShellExecTemplate;
6
7
class Caller
8
{
9
    const DEFAULT_OPENSSL_EXECUTABLE = 'openssl';
10
11
    /** @var string */
12
    private $executable;
13
14 100
    public function __construct(string $executable = '')
15
    {
16 100
        $this->executable = $executable ?: static::DEFAULT_OPENSSL_EXECUTABLE;
17 100
    }
18
19 17
    public function getExecutable(): string
20
    {
21 17
        return $this->executable;
22
    }
23
24 13
    public function call(string $template, array $arguments, array $environment = []): CallResponse
25
    {
26
        try {
27
            // build command for shellExec
28 13
            array_unshift($arguments, $this->getExecutable());
29 13
            $command = ($this->createShellExecTemplate())->create('? ' . $template, $arguments);
30
31
            // create ShellExec
32 13
            $shellExec = $this->createShellExec($command, $environment);
33 2
        } catch (\Throwable $exception) {
34 2
            throw new OpenSSLException('Unable to build command', 0, $exception);
35
        }
36
37
        // execute ShellExec
38 11
        $execution = $shellExec->run();
39
40
        // build response
41 11
        $callResponse = new CallResponse(
42 11
            $execution->commandLine(),
43 11
            $execution->output(),
44 11
            $execution->errors(),
45 11
            $execution->exitStatus()
46
        );
47
48
        // eval response
49 11
        if ($callResponse->exitStatus() !== 0) {
50 2
            throw new OpenSSLCallerException($callResponse);
51
        }
52
53 9
        return $callResponse;
54
    }
55
56 11
    protected function createShellExec(array $command, array $environment): ShellExec
57
    {
58 11
        return new ShellExec($command, $environment);
59
    }
60
61 13
    protected function createShellExecTemplate(): ShellExecTemplate
62
    {
63 13
        return new ShellExecTemplate();
64
    }
65
}
66