Completed
Push — master ( 338c5a...c10a26 )
by Mr
08:50
created

Generator::generateRaw()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 in array format
34
     *
35
     * @return array
36
     */
37 1
    private function generateArray(): array
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 : '');
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
            foreach ($routes as $route) {
55 1
                $config[] = 'route ' . $route;
56
            }
57 1
            foreach ($pushes as $push) {
58 1
                $config[] = 'push "' . $push . '"';
59
            }
60
        }
61
62
        // Certs should be below everything, due embedded keys and certificates
63 1
        if (count($certs) > 0) {
64 1
            foreach ($this->config->getCerts() as $key => $value) {
65 1
                $config[] .= isset($value['content'])
66
                    ? "<$key>\n{$value['content']}\n</$key>"
67 1
                    : "$key {$value['path']}";
68
            }
69
        }
70
71 1
        return $config;
72
    }
73
74
    /**
75
     * Generate config in JSON format
76
     *
77
     * @return string
78
     */
79
    private function generateJson(): string
80
    {
81
        $config = $this->generateArray();
82
        return json_encode($config, JSON_PRETTY_PRINT);
83
    }
84
85
    /**
86
     * Generate config in RAW format
87
     *
88
     * @return string
89
     */
90 1
    private function generateRaw(): string
91
    {
92 1
        $config = $this->generateArray();
93 1
        return implode(PHP_EOL, $config);
94
    }
95
96
    /**
97
     * Generate config by parameters in memory
98
     *
99
     * @param string $type Type of generated config: raw (default), json, array
100
     *
101
     * @return array|string|null
102
     */
103 1
    public function generate(string $type = 'raw')
104
    {
105 1
        if ($type === 'raw') {
106 1
            return $this->generateRaw();
107
        }
108
109
        if ($type === 'json') {
110
            return $this->generateJson();
111
        }
112
113
        if ($type === 'array') {
114
            return $this->generateArray();
115
        }
116
117
        return null;
118
    }
119
}
120