Completed
Push — symfony-console ( be5f04...81598e )
by Arnaud
03:48
created

CommandBuild::execute()   B

Complexity

Conditions 7
Paths 96

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.4106
c 0
b 0
f 0
cc 7
nc 96
nop 2
1
<?php
2
3
namespace Cecil\Command;
4
5
use Symfony\Component\Console\Input\InputDefinition;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class CommandBuild extends Command
11
{
12
    protected function configure()
13
    {
14
        $this
15
            ->setName('build')
16
            ->setDescription('Build the website')
17
            ->setHelp('Build the website in the output directory.')
18
            ->setDefinition(
19
                new InputDefinition([
20
                    new InputOption('drafts', 'd', InputOption::VALUE_NONE, 'Include drafts'),
21
                    new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Build without saving'),
22
                    new InputOption('baseurl', null, InputOption::VALUE_REQUIRED, 'Set the base URL'),
23
                    new InputOption('destination', null, InputOption::VALUE_REQUIRED, 'Set the output directory'),
24
                ])
25
            );
26
    }
27
28
    protected function execute(InputInterface $input, OutputInterface $output)
29
    {
30
        $config = [];
31
        $options = [];
32
33
        if ($input->getOption('drafts')) {
34
            $options['drafts'] = true;
35
            $messageOpt .= ' with drafts';
0 ignored issues
show
Bug introduced by
The variable $messageOpt does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
36
        }
37
        if ($input->getOption('dry-run')) {
38
            $options['dry-run'] = true;
39
            $messageOpt .= ' dry-run';
40
        }
41
        if ($input->getOption('baseurl')) {
42
            $config['site']['baseurl'] = $input->getOption('baseurl');
43
        }
44
        if ($input->getOption('destination')) {
45
            $config['site']['output']['dir'] = $input->getOption('destination');
46
            $this->fs->dumpFile($this->getPath() . '/' . Serve::$tmpDir . '/output', $input->getOption('destination'));
47
        }
48
49
        try {
50
            if (!$this->quiet) {
0 ignored issues
show
Bug introduced by
The property quiet does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
51
                $output->writeln(sprintf('Building website%s...', $messageOpt));
52
                $output->writeln(sprintf('<comment>Path: %s</comment>', $this->getPath()));
53
            }
54
            $this->getBuilder($output, $config, $options)->build($options);
55
            //if ($this->getRoute()->getName() == 'serve') {
56
            //    $this->fs->dumpFile($this->getPath() . '/' . Serve::$tmpDir . '/changes.flag', '');
57
            //}
58
        } catch (\Exception $e) {
59
            throw new \Exception(sprintf('%s', $e->getMessage()));
60
        }
61
62
        return 0;
63
    }
64
}
65