Worker   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 102
ccs 0
cts 40
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A getConfig() 0 4 1
B getContent() 0 28 6
A exec() 0 23 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EasyRSA;
6
7
use EasyRSA\Interfaces\ConfigInterface;
8
use EasyRSA\Interfaces\WorkerInterface;
9
use RuntimeException;
10
11
class Worker implements WorkerInterface
12
{
13
    /**
14
     * @var \EasyRSA\Interfaces\ConfigInterface
15
     */
16
    protected $config;
17
18
    /**
19
     * If need to enable debug mode
20
     */
21
    public $dryRun = false;
22
23
    /**
24
     * Wrapper constructor, need configuration for normal usage
25
     *
26
     * @param \EasyRSA\Interfaces\ConfigInterface|array $config
27
     *
28
     * @throws \RuntimeException
29
     */
30
    public function __construct($config = [])
31
    {
32
        if (is_array($config)) {
33
            $config = new Config($config);
34
        }
35
36
        if ($config instanceof ConfigInterface) {
37
            $this->config = $config;
38
        }
39
40
        // Must be set to enable ability execute shell scripts in background
41
        putenv("EASYRSA_PKI={$this->config->get('certs')}");
42
    }
43
44
    /**
45
     * @return \EasyRSA\Interfaces\ConfigInterface
46
     */
47
    public function getConfig(): ConfigInterface
48
    {
49
        return $this->config;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getContent(string $filename): ?string
56
    {
57
        switch ($filename) {
58
            case 'ca.crt':
59
            case 'dh.pem':
60
                $path = $this->config->get('certs') . '/' . $filename;
61
                break;
62
            default:
63
                $ext = pathinfo($filename, PATHINFO_EXTENSION);
64
                switch ($ext) {
65
                    case 'crt':
66
                        $path = $this->config->get('certs') . '/issued/' . $filename;
67
                        break;
68
                    case 'key':
69
                        $path = $this->config->get('certs') . '/private/' . $filename;
70
                        break;
71
                    case 'req':
72
                        $path = $this->config->get('certs') . '/reqs/' . $filename;
73
                        break;
74
                    default:
75
                        return null;
76
                        break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
77
                }
78
                break;
79
        }
80
81
        return file_get_contents($path);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     *
87
     * @throws \RuntimeException Of folder with certs unable to create or if is not a folder
88
     */
89
    public function exec(string $cmd): array
90
    {
91
        $command = $this->config->get('scripts') . '/easyrsa3/easyrsa --batch ' . $cmd;
92
93
        // In dry run mode need just return command without real execution
94
        if ($this->dryRun) {
95
            $result = [$command];
96
        } else {
97
            // Create folder if not exist
98
            if (
99
                !file_exists($this->config->get('certs'))
100
                && !mkdir($this->config->get('certs'), 0755, true)
101
                && !is_dir($this->config->get('certs'))
102
            ) {
103
                throw new RuntimeException("Folder \"{$this->config->get('certs')}\" can't be created");
104
            }
105
106
            chdir($this->config->get('certs'));
107
            exec($command, $result);
108
        }
109
110
        return $result;
111
    }
112
}
113