Completed
Push — code-style ( eb3614...00b548 )
by Arnaud
06:59 queued 03:13
created

Build::execute()   D

Complexity

Conditions 9
Paths 704

Size

Total Lines 51
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 9
eloc 35
c 3
b 0
f 0
nc 704
nop 2
dl 0
loc 51
rs 4.3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Command;
12
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputDefinition;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class Build extends Command
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected function configure()
25
    {
26
        $this
27
            ->setName('build')
28
            ->setDescription('Build the website')
29
            ->setDefinition(
30
                new InputDefinition([
31
                    new InputArgument('path', InputArgument::OPTIONAL, 'Use the given path as working directory'),
32
                    new InputOption('drafts', 'd', InputOption::VALUE_NONE, 'Include drafts'),
33
                    new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Build without saving'),
34
                    new InputOption('baseurl', null, InputOption::VALUE_REQUIRED, 'Set the base URL'),
35
                    new InputOption('destination', null, InputOption::VALUE_REQUIRED, 'Set the output directory'),
36
                    new InputOption(
37
                        'optimize',
38
                        null,
39
                        InputOption::VALUE_OPTIONAL,
40
                        'Optimize output (disable with "no")',
41
                        false
42
                    ),
43
                ])
44
            )
45
            ->setHelp('Build the website in the output directory.');
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53
        $config = [];
54
        $options = [];
55
        $messageOpt = '';
56
57
        if ($input->getOption('drafts')) {
58
            $options['drafts'] = true;
59
            $messageOpt .= ' with drafts';
60
        }
61
        if ($input->getOption('dry-run')) {
62
            $options['dry-run'] = true;
63
            $messageOpt .= ' dry-run';
64
        }
65
        if ($input->getOption('baseurl')) {
66
            $config['baseurl'] = $input->getOption('baseurl');
67
        }
68
        if ($input->getOption('destination')) {
69
            $config['output']['dir'] = $input->getOption('destination');
70
            $this->fs->dumpFile(
71
                $this->getPath().'/'.self::TMP_DIR.'/output',
72
                (string) $input->getOption('destination')
73
            );
74
        }
75
        if ($input->getOption('optimize') === null) {
76
            $config['optimize']['enabled'] = true;
77
        }
78
        if ($input->getOption('optimize') == 'no') {
79
            $config['optimize']['enabled'] = false;
80
        }
81
82
        try {
83
            $output->writeln(sprintf('Building website%s...', $messageOpt));
84
            $output->writeln(
85
                sprintf('<comment>Path: %s</comment>', $this->getPath()),
86
                OutputInterface::VERBOSITY_VERBOSE
87
            );
88
            $builder = $this->getBuilder($output, $config);
89
            if ((bool) $this->builder->getConfig()->get('cache.enabled')) {
90
                $output->writeln(
91
                    sprintf('<comment>Cache: %s</comment>', $this->builder->getConfig()->getCachePath()),
92
                    OutputInterface::VERBOSITY_VERBOSE
93
                );
94
            }
95
            $builder->build($options);
96
            $this->fs->dumpFile($this->getPath().'/'.self::TMP_DIR.'/changes.flag', time());
97
        } catch (\Exception $e) {
98
            throw new \Exception(sprintf('%s', $e->getMessage()));
99
        }
100
101
        return 0;
102
    }
103
}
104