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

Caller   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 57
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createShellExecTemplate() 0 3 1
A createShellExec() 0 3 1
A getExecutable() 0 3 1
A call() 0 30 3
A __construct() 0 3 2
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