Completed
Push — master ( 1184b8...684030 )
by Danny
05:24
created

RabbitMqConfigGenerator::write()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
namespace MyOnlineStore\Bundle\RabbitMqManagerBundle\Generator;
4
5
use Indigo\Ini\Renderer;
6
use League\Flysystem\FilesystemInterface;
7
use MyOnlineStore\Bundle\RabbitMqManagerBundle\Configuration\Consumer\ConsumerConfiguration;
8
use MyOnlineStore\Bundle\RabbitMqManagerBundle\Configuration\Supervisor\SupervisorConfiguration;
9
10
class RabbitMqConfigGenerator implements RabbitMqConfigGeneratorInterface
11
{
12
    /**
13
     * @var SupervisorConfiguration
14
     */
15
    private $supervisorConfiguration;
16
17
    /**
18
     * @var ConsumerConfiguration
19
     */
20
    private $consumerConfiguration;
21
22
    /**
23
     * @var Renderer
24
     */
25
    private $renderer;
26
27
    /**
28
     * @var FilesystemInterface
29
     */
30
    private $filesystem;
31
32
    /**
33
     * @var array
34
     */
35
    private $config;
36
37
    /**
38
     * @param SupervisorConfiguration $supervisorConfiguration
39
     * @param ConsumerConfiguration   $consumerConfiguration
40
     * @param Renderer                $renderer
41
     * @param FilesystemInterface     $filesystem
42
     * @param array                   $config
43
     */
44 4
    public function __construct(
45
        SupervisorConfiguration $supervisorConfiguration,
46
        ConsumerConfiguration $consumerConfiguration,
47
        Renderer $renderer,
48
        FilesystemInterface $filesystem,
49
        array $config
50
    ) {
51 4
        $this->supervisorConfiguration = $supervisorConfiguration;
52 4
        $this->consumerConfiguration = $consumerConfiguration;
53 4
        $this->renderer = $renderer;
54 4
        $this->filesystem = $filesystem;
55 4
        $this->config = $config;
56 4
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61 4
    public function generate()
62
    {
63 4
        $supervisorConfiguration = $this->supervisorConfiguration->generate();
64
65 4
        foreach (['consumers', 'rpc_servers'] as $type) {
66 4
            foreach ($this->config[$type] as $consumerName => $consumer) {
67 3
                if (!isset($consumer['worker']['queue']['routing'])) {
68
                    // this can be moved to DI\Configuration
69
                    $consumer['worker']['queue']['routing'] = [null];
70
                }
71
72 3
                foreach ($consumer['worker']['queue']['routing'] as $index => $route) {
73 3
                    $name = sprintf('%s_%s_%d', substr($type, 0, 1), $consumerName, $index);
74
75 3
                    if ('cli-consumer' === $consumer['processor']) {
76 1
                        $this->write(
77 1
                            sprintf('%s.conf', $name),
78 1
                            $this->renderer->render($this->consumerConfiguration->generate($consumer, $route)->toArray())
79 1
                        );
80
81 1
                        $supervisorConfiguration->addSection(
82 1
                            $this->supervisorConfiguration->generateProgram(
83 1
                                $name,
84 1
                                $this->supervisorConfiguration->getConsumerProperties(
85 1
                                    $consumer,
86 1
                                    sprintf('%s/%s.conf', $this->config['path'], $name)
87 1
                                )
88 1
                            )
89 1
                        );
90 1
                    } else {
91 2
                        $supervisorConfiguration->addSection(
92 2
                            $this->supervisorConfiguration->generateProgram(
93 2
                                $name,
94 2
                                $this->supervisorConfiguration->getBundleProperties($consumer, $route)
95 2
                            )
96 2
                        );
97
                    };
98 3
                }
99 4
            }
100 4
        }
101
102 4
        $this->write('supervisord.conf', $this->renderer->render(
103 4
            $supervisorConfiguration->toArray()
104 4
        ));
105 4
    }
106
107
    /**
108
     * @param string $path
109
     * @param string $content
110
     */
111 4
    private function write($path, $content)
112
    {
113 4
        if ($this->filesystem->has($path)) {
114 2
            $this->filesystem->update($path, $content);
115 2
        } else {
116 3
            $this->filesystem->write($path, $content);
117
        }
118 4
    }
119
}
120