Completed
Push — master ( e54095...b55a89 )
by Mr
02:48
created

Generator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace OpenVPN;
4
5
use OpenVPN\Interfaces\ConfigInterface;
6
use OpenVPN\Interfaces\GeneratorInterface;
7
use function count;
8
9
/**
10
 * Class Generator
11
 *
12
 * @package OpenVPN
13
 * @since   1.0.0
14
 */
15
class Generator implements GeneratorInterface
16
{
17
    /**
18
     * @var \OpenVPN\Interfaces\ConfigInterface
19
     */
20
    private $config;
21
22
    /**
23
     * Generator constructor.
24
     *
25
     * @param \OpenVPN\Interfaces\ConfigInterface $config
26
     */
27 1
    public function __construct(ConfigInterface $config)
28
    {
29 1
        $this->config = $config;
30 1
    }
31
32
    /**
33
     * Generate config by parameters in memory
34
     *
35
     * @return string
36
     */
37 1
    public function generate(): string
38
    {
39
        // Init the variable
40 1
        $config = '';
41
42
        // Basic parameters first
43 1
        foreach ($this->config->getParameters() as $key => $value) {
44 1
            $config .= $key . ($value !== '' ? ' ' . $value : '') . "\n";
45
        }
46
47
        // Get all what need for normal work
48 1
        $pushes = $this->config->getPushes();
49 1
        $routes = $this->config->getRoutes();
50 1
        $certs  = $this->config->getCerts();
51
52
        // If we have routes or pushes in lists then generate it
53 1
        if (count($pushes) || count($routes)) {
54 1
            $config .= "\n### Networking\n";
55 1
            foreach ($routes as $route) {
56 1
                $config .= 'route ' . $route . "\n";
57
            }
58 1
            foreach ($pushes as $push) {
59 1
                $config .= 'push "' . $push . "\"\n";
60
            }
61
        }
62
63
        // Certs should be below everything, due embedded keys and certificates
64 1
        if (count($certs) > 0) {
65 1
            $config .= "\n### Certificates\n";
66 1
            foreach ($this->config->getCerts() as $key => $value) {
67 1
                $config .= isset($value['content'])
68
                    ? "<$key>\n{$value['content']}\n</$key>\n"
69 1
                    : "$key {$value['path']}\n";
70
            }
71
        }
72
73 1
        return $config;
74
    }
75
}
76