Completed
Push — master ( c10a26...3a195b )
by Mr
11:23
created

Generator::generateJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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