Completed
Push — master ( 532b1f...89ab0b )
by Mr
04:53 queued 11s
created

Generator::generate()   B

Complexity

Conditions 10
Paths 12

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 7.6666
c 0
b 0
f 0
cc 10
nc 12
nop 0

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
    public function __construct(ConfigInterface $config)
28
    {
29
        $this->config = $config;
30
    }
31
32
    /**
33
     * Generate config by parameters in memory
34
     *
35
     * @return string
36
     */
37
    public function generate(): 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 : '') . "\n";
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
            $config .= "\n### Networking\n";
55
            foreach ($routes as $route) {
56
                $config .= 'route ' . $route . "\n";
57
            }
58
            foreach ($pushes as $push) {
59
                $config .= 'push "' . $push . "\"\n";
60
            }
61
        }
62
63
        // Certs should be below everything, due embedded keys and certificates
64
        if (count($certs) > 0) {
65
            $config .= "\n### Certificates\n";
66
            foreach ($this->config->getCerts() as $key => $value) {
67
                $config .= isset($value['content'])
68
                    ? "<$key>\n{$value['content']}\n</$key>\n"
69
                    : "$key {$value['path']}\n";
70
            }
71
        }
72
73
        return $config;
74
    }
75
}
76