Completed
Push — master ( b55a89...338c5a )
by Mr
07:01
created

Generator::generateJson()   B

Complexity

Conditions 10
Paths 12

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 0
cts 18
cp 0
rs 7.6666
c 0
b 0
f 0
cc 10
nc 12
nop 0
crap 110

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 JSON format
34
     *
35
     * @return string
36
     */
37
    private function generateJson(): string
38
    {
39
        // Init the variable
40
        $config = [];
41
42
        // Basic parameters first
43
        foreach ($this->config->getParameters() as $key => $value) {
44
            $config[] = $key . ($value !== '' ? ' ' . $value : '');
45
        }
46
47
        // Get all what need for normal work
48
        $pushes = $this->config->getPushes();
49
        $routes = $this->config->getRoutes();
50
        $certs  = $this->config->getCerts();
51
52
        // If we have routes or pushes in lists then generate it
53
        if (count($pushes) || count($routes)) {
54
            foreach ($routes as $route) {
55
                $config[] = 'route ' . $route;
56
            }
57
            foreach ($pushes as $push) {
58
                $config[] = 'push "' . $push . '"';
59
            }
60
        }
61
62
        // Certs should be below everything, due embedded keys and certificates
63
        if (count($certs) > 0) {
64
            foreach ($this->config->getCerts() as $key => $value) {
65
                $config[] .= isset($value['content'])
66
                    ? "<$key>\n{$value['content']}\n</$key>"
67
                    : "$key {$value['path']}";
68
            }
69
        }
70
71
        return json_encode($config, JSON_PRETTY_PRINT);
72
    }
73
74
    /**
75
     * Generate config in RAW format
76
     *
77
     * @return string
78
     */
79 1
    private function generateRaw(): string
80
    {
81
        // Init the variable
82 1
        $config = null;
83
84
        // Basic parameters first
85 1
        foreach ($this->config->getParameters() as $key => $value) {
86 1
            $config .= $key . ($value !== '' ? ' ' . $value : '') . "\n";
87
        }
88
89
        // Get all what need for normal work
90 1
        $pushes = $this->config->getPushes();
91 1
        $routes = $this->config->getRoutes();
92 1
        $certs  = $this->config->getCerts();
93
94
        // If we have routes or pushes in lists then generate it
95 1
        if (count($pushes) || count($routes)) {
96 1
            $config .= "\n### Networking\n";
97 1
            foreach ($routes as $route) {
98 1
                $config .= 'route ' . $route . "\n";
99
            }
100 1
            foreach ($pushes as $push) {
101 1
                $config .= 'push "' . $push . "\"\n";
102
            }
103
        }
104
105
        // Certs should be below everything, due embedded keys and certificates
106 1
        if (count($certs) > 0) {
107 1
            $config .= "\n### Certificates\n";
108 1
            foreach ($this->config->getCerts() as $key => $value) {
109 1
                $config .= isset($value['content'])
110
                    ? "<$key>\n{$value['content']}\n</$key>\n"
111 1
                    : "$key {$value['path']}\n";
112
            }
113
        }
114
115 1
        return $config;
116
    }
117
118
    /**
119
     * Generate config by parameters in memory
120
     *
121
     * @param string $type Type of generated config: raw (default), json
122
     *
123
     * @return string|null
124
     */
125 1
    public function generate(string $type = 'raw'): ?string
126
    {
127 1
        if ($type === 'raw') {
128 1
            return $this->generateRaw();
129
        }
130
131
        if ($type === 'json') {
132
            return $this->generateJson();
133
        }
134
135
        return null;
136
    }
137
}
138