Completed
Push — master ( ca39d5...12bd24 )
by Danny
10:03
created

RabbitMqConfigGenerator::getProgramSection()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 54
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 0
cts 45
cp 0
rs 9.0306
c 0
b 0
f 0
cc 4
eloc 37
nc 6
nop 4
crap 20

How to fix   Long Method   

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 MyOnlineStore\Bundle\RabbitMqManagerBundle\Generator;
4
5
use Indigo\Ini\Renderer;
6
use MyOnlineStore\Bundle\RabbitMqManagerBundle\Configuration\Consumer\ConsumerConfiguration;
7
use MyOnlineStore\Bundle\RabbitMqManagerBundle\Configuration\Supervisor\SupervisorConfiguration;
8
9
class RabbitMqConfigGenerator implements RabbitMqConfigGeneratorInterface
10
{
11
    /**
12
     * @var SupervisorConfiguration
13
     */
14
    private $supervisorConfiguration;
15
16
    /**
17
     * @var ConsumerConfiguration
18
     */
19
    private $consumerConfiguration;
20
21
    /**
22
     * @var Renderer
23
     */
24
    private $renderer;
25
26
    /**
27
     * @var array
28
     */
29
    private $config;
30
31
    /**
32
     * @param SupervisorConfiguration $supervisorConfiguration
33
     * @param ConsumerConfiguration   $consumerConfiguration
34
     * @param Renderer                $renderer
35
     * @param array                   $config
36
     */
37
    public function __construct(
38
        SupervisorConfiguration $supervisorConfiguration,
39
        ConsumerConfiguration $consumerConfiguration,
40
        Renderer $renderer,
41
        array $config
42
    ) {
43
        $this->supervisorConfiguration = $supervisorConfiguration;
44
        $this->consumerConfiguration = $consumerConfiguration;
45
        $this->renderer = $renderer;
46
        $this->config = $config;
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function generate()
53
    {
54
        if (!is_dir($this->config['path']) && !mkdir($this->config['path'], 0755, true)) {
55
            throw new \RuntimeException(
56
                sprintf(
57
                    'path "%s" could not be created',
58
                    $this->config['path']
59
                )
60
            );
61
        }
62
63
        $supervisorConfiguration = $this->supervisorConfiguration->generate();
64
65
        foreach (['consumers', 'rpc_servers'] as $type) {
66
            foreach ($this->config[$type] as $name => $consumer) {
67
                if (!isset($consumer['worker']['queue']['routing'])) {
68
                    // this can be moved to DI\Configuration
69
                    $consumer['worker']['queue']['routing'] = [null];
70
                }
71
72
                foreach ($consumer['worker']['queue']['routing'] as $index => $route) {
73
                    $name = sprintf('%s_%s_%d', substr($type, 0, 1), $name, $index);
74
75
                    if ('cli-consumer' === $consumer['processor']) {
76
                        $consumerConfiguration = sprintf('%s/%s.conf', $this->config['path'], $name);
77
78
                        file_put_contents($consumerConfiguration, $this->renderer->render(
79
                            $this->consumerConfiguration->generate($consumer, $route)->toArray())
80
                        );
81
82
                        $supervisorConfiguration->addSection(
83
                            $this->supervisorConfiguration->generateProgram(
84
                                $name,
85
                                $this->supervisorConfiguration->getConsumerProperties(
86
                                    $consumer,
87
                                    $consumerConfiguration
88
                                )
89
                            )
90
                        );
91
                    } else {
92
                        $supervisorConfiguration->addSection(
93
                            $this->supervisorConfiguration->generateProgram(
94
                                $name,
95
                                $this->supervisorConfiguration->getBundleProperties($consumer, $route)
96
                            )
97
                        );
98
                    };
99
                }
100
            }
101
        }
102
103
        file_put_contents(sprintf('%s/supervisord.conf', $this->config['path']), $this->renderer->render(
104
            $supervisorConfiguration->toArray()
105
        ));
106
    }
107
}
108