Passed
Branch release/v3.0.0 (5cec78)
by Anatoly
03:23
created

GenerateRoadRunnerSystemdUnitCommand::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 20
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 28
ccs 21
cts 21
cp 1
crap 1
rs 9.6
1
<?php declare(strict_types=1);
2
3
namespace App\Command;
4
5
/**
6
 * Import classes
7
 */
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Question\Question;
12
13
/**
14
 * Import functions
15
 */
16
use function sprintf;
17
use function strtr;
18
use function trim;
19
20
/**
21
 * GenerateRoadRunnerSystemdUnitCommand
22
 *
23
 * @link https://wiki.debian.org/systemd/Services
24
 * @link https://manpages.debian.org/buster/systemd/systemd.service.5.en.html
25
 */
26
final class GenerateRoadRunnerSystemdUnitCommand extends Command
27
{
28
29
    /**
30
     * @var string
31
     */
32
    private const TEMPLATE = <<<'EOT'
33
[Unit]
34
After=network.target
35
36
[Service]
37
Type=simple
38
User={user}
39
Group={group}
40
Restart=always
41
WorkingDirectory={cwd}
42
ExecStart={rr} serve -dv
43
ExecReload={rr} http:reset
44
ExecStop=/bin/kill -s TERM $MAINPID
45
46
[Install]
47
WantedBy=multi-user.target
48
EOT;
49
50
    /**
51
     * {@inheritDoc}
52
     */
53 2
    protected function configure() : void
54
    {
55 2
        $this->setName('app:roadrunner:generate-systemd-unit');
56 2
        $this->setDescription('Generates the systemd unit for RoadRunner');
57 2
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62 1
    protected function execute(InputInterface $input, OutputInterface $output) : int
63
    {
64 1
        $rr = '/usr/local/bin/rr';
65 1
        $cwd = trim(`pwd`);
66 1
        $user = trim(`id -u -n`);
67 1
        $group = trim(`id -g -n`);
68 1
        $questioner = $this->getHelper('question');
69 1
        $replacements = [];
70
71 1
        $format = 'RR path [<fg=yellow>%s</>]: ';
72 1
        $question = new Question(sprintf($format, $rr), $rr);
73 1
        $replacements['{rr}'] = $questioner->ask($input, $output, $question);
74
75 1
        $format = 'App root [<fg=yellow>%s</>]: ';
76 1
        $question = new Question(sprintf($format, $cwd), $cwd);
77 1
        $replacements['{cwd}'] = $questioner->ask($input, $output, $question);
78
79 1
        $format = 'User name [<fg=yellow>%s</>]: ';
80 1
        $question = new Question(sprintf($format, $user), $user);
81 1
        $replacements['{user}'] = $questioner->ask($input, $output, $question);
82
83 1
        $format = 'Group name [<fg=yellow>%s</>]: ';
84 1
        $question = new Question(sprintf($format, $group), $group);
85 1
        $replacements['{group}'] = $questioner->ask($input, $output, $question);
86
87 1
        $output->writeln(strtr(static::TEMPLATE, $replacements));
88
89 1
        return 0;
90
    }
91
}
92