Completed
Push — master ( b72fb2...ff6b5c )
by Carlos C
16s queued 13s
created

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