Completed
Push — master ( 06dc35...966f49 )
by Danny
05:47
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 26
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 MyOnlineStore\Bundle\RabbitMqManagerBundle\Process\ProcessBuilderFactoryInterface;
6
use Supervisor\Configuration\Section\RpcInterface;
7
use Supervisor\Configuration\Section\Supervisorctl;
8
use Supervisor\Configuration\Section\UnixHttpServer;
9
use Supervisor\Configuration\Configuration;
10
use Supervisor\Configuration\Section\Supervisord;
11
use Supervisor\Configuration\Section\Program;
12
use Indigo\Ini\Renderer;
13
use Symfony\Component\Templating\EngineInterface;
14
15
class RabbitMqConfigGenerator implements RabbitMqConfigGeneratorInterface
16
{
17
    /**
18
     * @var array
19
     */
20
    private $config;
21
    /**
22
     * @var ProcessBuilderFactoryInterface
23
     */
24
    private $processBuilderFactory;
25
    /**
26
     * @var EngineInterface
27
     */
28
    private $templating;
29
30
    /**
31
     * @param ProcessBuilderFactoryInterface $processBuilderFactory
32
     * @param EngineInterface                $templating
33
     * @param array                          $config
34
     */
35
    public function __construct(ProcessBuilderFactoryInterface $processBuilderFactory, EngineInterface $templating, array $config)
36
    {
37
        $this->config = $config;
38
        $this->processBuilderFactory = $processBuilderFactory;
39
        $this->templating = $templating;
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function generate()
46
    {
47
        if (!is_dir($this->config['path']) && !mkdir($this->config['path'], 0755, true)) {
48
            throw new \RuntimeException(sprintf(
49
                'path "%s" could not be created',
50
                $this->config['path']
51
            ));
52
        }
53
54
        $configuration = new Configuration();
55
        $renderer = new Renderer();
56
57
        $configuration->addSection(new UnixHttpServer([
58
            'file' => sprintf('%s/supervisord.sock', $this->config['path']),
59
            'chmod' => 700,
60
        ]));
61
62
        $configuration->addSection(new Supervisord([
63
            'logfile' => sprintf('%s/supervisord.log', $this->config['path']),
64
            'pidfile' => sprintf('%s/supervisord.pid', $this->config['path']),
65
        ]));
66
67
        $configuration->addSection(new RpcInterface('supervisor', [
68
            'supervisor.rpcinterface_factory' => 'supervisor.rpcinterface:make_main_rpcinterface',
69
        ]));
70
71
        $configuration->addSection(new Supervisorctl([
72
            'serverurl' => sprintf('unix://%s/supervisord.sock', $this->config['path']),
73
        ]));
74
75
        foreach (['consumers', 'rpc_servers'] as $type) {
76
            foreach ($this->config[$type] as $name => $consumer) {
77
                if (!isset($consumer['worker']['queue']['routing'])) {
78
                    $configuration->addSection($this->getProgramSection(
79
                        sprintf('%s_%s', $type, $name),
80
                        $this->config['path'],
81
                        $consumer
82
                    ));
83
84
                    continue;
85
                }
86
87
                foreach ($consumer['worker']['queue']['routing'] as $index => $route) {
88
                    $configuration->addSection($this->getProgramSection(
89
                        sprintf('%s_%s_%s', $type, $name, $index),
90
                        $this->config['path'],
91
                        $consumer,
92
                        $route
93
                    ));
94
                }
95
            }
96
        }
97
98
        file_put_contents(
99
            sprintf('%s/%s', $this->config['path'], 'supervisord.conf'),
100
            $renderer->render($configuration->toArray())
101
        );
102
    }
103
104
    /**
105
     * @param string $name
106
     * @param string $path
107
     * @param array  $consumer
108
     * @param null   $route
109
     *
110
     * @return Program
111
     */
112
    public function getProgramSection($name, $path, array $consumer, $route = null)
113
    {
114
        $processBuilder = $this->processBuilderFactory->create();
115
        $processBuilder->setPrefix(['php']);
116
        $processBuilder->add($consumer['command']['console']);
117
118
        foreach ($consumer['command']['arguments'] as $argument) {
119
            $processBuilder->add($argument);
120
        }
121
122
        if ('cli-consumer' === $consumer['processor']) {
123
            // write additional cli-consumer config
124
            file_put_contents(
125
                $consumerConfiguration = sprintf('%s/%s.conf', $path, $name),
126
                $this->templating->render('RabbitMqManagerBundle:Supervisor:consumer.conf.twig', [
127
                    'path' => $path,
128
                    'routing_key' => $route,
129
                    'consumer' => $consumer,
130
                ])
131
            );
132
133
            $processBuilder->add($consumer['command']['command']);
134
            $processBuilder->add($consumer['name']);
135
136
            $consumerProcessBuilder = $this->processBuilderFactory->create();
137
            $consumerProcessBuilder->setPrefix(['rabbitmq-cli-consumer']);
138
            $consumerProcessBuilder->add('--strict-exit-code');
139
            $consumerProcessBuilder->add('--include');
140
            $consumerProcessBuilder->add(sprintf('--configuration=%s', $consumerConfiguration));
141
            $consumerProcessBuilder->add(sprintf('--executable=%s', $processBuilder->getProcess()->getCommandLine()));
142
143
            $process = $consumerProcessBuilder->getProcess();
144
        } else {
145
            $processBuilder->add(sprintf('--messages=%s', $consumer['messages']));
146
147
            if (null !== $route) {
148
                $processBuilder->add(sprintf('--route=%s', $route));
149
            }
150
151
            $processBuilder->add($consumer['command']['command']);
152
            $processBuilder->add($consumer['name']);
153
            $process = $processBuilder->getProcess();
154
        }
155
156
        return new Program($name, [
157
            'command' => $process->getCommandLine(),
158
            'process_name' => '%(program_name)s%(process_num)02d',
159
            'numprocs' => $consumer['supervisor']['count'],
160
            'startsecs' => $consumer['supervisor']['startsecs'],
161
            'autorestart' => $consumer['supervisor']['autorestart'],
162
            'stopsignal' => $consumer['supervisor']['stopsignal'],
163
            'stopwaitsecs' => $consumer['supervisor']['stopwaitsecs'],
164
        ]);
165
    }
166
}
167