Test Failed
Pull Request — master (#31)
by Sébastien
02:57
created

GenerateSupervisorConfigurationFiles::retrieveServer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 0
cts 9
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 6
1
<?php
2
3
namespace Puzzle\AMQP\Commands;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Puzzle\AMQP\Workers\WorkerProvider;
9
use Gaufrette\Filesystem;
10
use Gaufrette\Adapter\Local;
11
use Puzzle\AMQP\Services\SupervisorConfigurationGenerator;
12
use Puzzle\AMQP\Services\CommandGenerator;
13
use Symfony\Component\Console\Input\InputOption;
14
use Puzzle\Pieces\ExtraInformation;
15
use Puzzle\Pieces\PathManipulation;
16
17
class GenerateSupervisorConfigurationFiles extends Command
18
{
19
    use
20
        ExtraInformation,
21
        PathManipulation;
22
23
    private
24
        $appId,
25
        $workerProvider,
26
        $commandGenerator;
27
28
    public function __construct(WorkerProvider $workerProvider, $appId)
29
    {
30
        parent::__construct();
31
32
        $this->workerProvider = $workerProvider;
33
        $this->commandGenerator = new CommandGenerator();
34
        $this->appId = (string) $appId;
35
    }
36
37
    protected function configure()
38
    {
39
        $this
40
            ->setName('generate:supervisord:configuration')
41
            ->setDescription('Generate supervisord configuration for workers.')
42
            ->addOption('destination', null, InputOption::VALUE_REQUIRED, '[REQUIRED] The directory where to write the generated configuration file.')
43
            ->addOption('autostart', null, InputOption::VALUE_REQUIRED, 'Configure the autostart value. (default true)', 'true')
44
            ->addOption('autorestart', null, InputOption::VALUE_REQUIRED, 'Configure the autorestart value. (default true)', 'true')
45
            ->addOption('quiet', 'q', InputOption::VALUE_NONE, 'Quiet mode.')
46
        ;
47
    }
48
49
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51
        $this->startTimer();
52
53
        $this->process($input, $output);
54
55
        $this->endTimer();
56
        $this->outputExtraInformation($output);
57
    }
58
59
    private function process(InputInterface $input, OutputInterface $output)
60
    {
61
        $this->checkRequirements($input);
62
        $destination = $this->retrieveDestination($input, $output);
63
64
        $workers = $this->workerProvider->listAll();
65
66
        $autostart = $this->retrieveBoolean('autostart', $input);
67
        $autorestart = $this->retrieveboolean('autorestart', $input);
68
69
        $fs = new Filesystem(new Local($destination, true));
70
        $supervisorConfigurationGenerator = new SupervisorConfigurationGenerator($fs, $this->commandGenerator);
71
        $supervisorConfigurationGenerator->generate($workers, $autostart, $autorestart, $this->appId, $destination, $output);
72
    }
73
74
    private function retrieveboolean($name, InputInterface $input)
75
    {
76
        $value = $input->getOption($name);
77
78
        if(! in_array($value, ['true', 'false']))
79
        {
80
            throw new \InvalidArgumentException(sprintf('Invalid value "%s" for option %s, expecting boolean.', $value, $name));
81
        }
82
83
        return $value === 'true';
84
    }
85
86
    private function checkRequirements(InputInterface $input)
87
    {
88
        $destination = $input->getOption('destination');
89
        if(empty($destination))
90
        {
91
            throw new \InvalidArgumentException('The option --destination is required.');
92
        }
93
    }
94
95
    private function retrieveDestination(InputInterface $input, OutputInterface $output)
96
    {
97
        $destination = $this->enforceEndingSlash($input->getOption('destination'));
98
99
        if( ! $input->getOption('quiet'))
100
        {
101
            $output->writeln(sprintf('<info>Destination : <comment>%s</comment></info>', $destination));
102
        }
103
104
        return $destination;
105
    }
106
}
107