VhostCreateCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 46.94%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 79
ccs 23
cts 49
cp 0.4694
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B configure() 0 28 1
B execute() 0 41 4
1
<?php
2
3
namespace Portiere\Command;
4
5
use Portiere\WebServer\ManagerFactory;
6
use Portiere\WebServer\Vhost;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
14
/**
15
 * Class VhostCreateCommand.
16
 *
17
 * @author Ignacio Velazquez <[email protected]>
18
 */
19
class VhostCreateCommand extends Command
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24 3
    protected function configure()
25
    {
26 3
        $this
27 3
            ->setName('vhost:create')
28 3
            ->setDescription('Creates a vhost for this project')
29 3
            ->addArgument(
30 3
                'serverName',
31 3
                InputArgument::REQUIRED,
32
                'Server name of the virtual host'
33 3
            )
34 3
            ->addArgument(
35 3
                'documentRoot',
36 3
                InputArgument::REQUIRED,
37
                'Path to the project\'s directory'
38 3
            )
39 3
            ->addOption(
40 3
                'vhost-filename',
41 3
                'vf',
42 3
                InputOption::VALUE_OPTIONAL,
43
                'Filename of the virtual host'
44 3
            )
45 3
            ->addOption(
46 3
                'no-dev',
47 3
                'no-dev',
48 3
                InputOption::VALUE_NONE,
49
                'Don\'t add Symfony2 dev front controller'
50 3
            );
51 3
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected function execute(InputInterface $input, OutputInterface $output)
57
    {
58
        $io = new SymfonyStyle($input, $output);
59
60
        $vhostFilename = $input->getOption('vhost-filename');
61
62
        if (null === $vhostFilename) {
63
            $vhostFilename = $input->getArgument('serverName');
64
        }
65
66
        $vhost = new Vhost($vhostFilename);
67
        $vhost
68
            ->setServerName($input->getArgument('serverName'))
69
            ->setDocumentRoot($input->getArgument('documentRoot'))
70
            ->setEnv($input->getOption('no-dev') ? Vhost::ENV_PROD : Vhost::ENV_DEV);
71
72
        $manager = ManagerFactory::create();
73
74
        // Dumping a preview
75
        $io->newLine();
76
        $io->writeln("The vhost file <info>{$manager->getVhostAvailablePath($vhost)}</info> will look like:");
77
        $io->newLine();
78
79
        $io->text($manager->getTemplate($vhost));
0 ignored issues
show
Security Bug introduced by
It seems like $manager->getTemplate($vhost) targeting Portiere\WebServer\NginxManager::getTemplate() can also be of type false; however, Symfony\Component\Consol...le\SymfonyStyle::text() does only seem to accept string|array, did you maybe forget to handle an error condition?
Loading history...
80
81
        if (!$io->confirm('Do you confirm the vhost generation?', true)) {
82
            $io->warning('Canceled!! The vhost has not been created due to user interruption');
83
84
            return;
85
        }
86
87
        $manager->createVhost($vhost);
88
        $manager->restartServer();
89
90
        $io->success('Your vhost has been successfully created and enabled');
91
92
        $io->newLine();
93
        $output->writeln('You should append this line to your <info>/etc/hosts</info> file:');
94
        $io->newLine();
95
        $output->writeln("<info>127.0.0.1\t{$vhost->getServerName()}</info>\n");
96
    }
97
}
98