Passed
Push — feature/config-file ( 80a3c9...c343d6 )
by Arnaud
11:33 queued 07:45
created

Build::execute()   D

Complexity

Conditions 10
Paths 512

Size

Total Lines 60
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 39
nc 512
nop 2
dl 0
loc 60
rs 4.1777
c 2
b 0
f 0

How to fix   Long Method    Complexity   

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 Cecil\Util;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputDefinition;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
/**
21
 * Builds the website.
22
 */
23
class Build extends AbstractCommand
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function configure()
29
    {
30
        $this
31
            ->setName('build')
32
            ->setDescription('Builds the website')
33
            ->setDefinition(
34
                new InputDefinition([
35
                    new InputArgument('path', InputArgument::OPTIONAL, 'Use the given path as working directory'),
36
                    new InputOption('config', 'c', InputOption::VALUE_REQUIRED, 'Specific configuration file ("config.yml")'),
37
                    new InputOption('drafts', 'd', InputOption::VALUE_NONE, 'Include drafts'),
38
                    new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Build without saving'),
39
                    new InputOption('baseurl', null, InputOption::VALUE_REQUIRED, 'Set the base URL'),
40
                    new InputOption('output', null, InputOption::VALUE_REQUIRED, 'Set the output directory'),
41
                    new InputOption(
42
                        'postprocess',
43
                        null,
44
                        InputOption::VALUE_OPTIONAL,
45
                        'Post-process output (disable with "no")',
46
                        false
47
                    ),
48
                    new InputOption('clear-cache', null, InputOption::VALUE_NONE, 'Clear cache after build'),
49
                ])
50
            )
51
            ->setHelp('Builds the website in the output directory');
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected function execute(InputInterface $input, OutputInterface $output)
58
    {
59
        $config = [];
60
        $options = [];
61
        $messageOpt = '';
62
63
        if ($input->getOption('drafts')) {
64
            $options['drafts'] = true;
65
            $messageOpt .= ' with drafts';
66
        }
67
        if ($input->getOption('dry-run')) {
68
            $options['dry-run'] = true;
69
            $messageOpt .= ' (dry-run)';
70
        }
71
        if ($input->getOption('baseurl')) {
72
            $config['baseurl'] = $input->getOption('baseurl');
73
        }
74
        if ($input->getOption('output')) {
75
            $config['output']['dir'] = $input->getOption('output');
76
            $this->fs->dumpFile(
77
                Util::joinFile($this->getPath(), self::TMP_DIR, 'output'),
78
                (string) $input->getOption('output')
79
            );
80
        }
81
        if ($input->getOption('postprocess') === null) {
82
            $config['postprocess']['enabled'] = true;
83
        }
84
        if ($input->getOption('postprocess') == 'no') {
85
            $config['postprocess']['enabled'] = false;
86
        }
87
        if ($input->getOption('clear-cache')) {
88
            $config['cache']['enabled'] = false;
89
        }
90
91
        $output->writeln(sprintf('Building website%s...', $messageOpt));
92
        $output->writeln(
93
            sprintf('<comment>Path: %s</comment>', $this->getPath()),
94
            OutputInterface::VERBOSITY_VERBOSE
95
        );
96
        if ($this->getConfigFile() !== null) {
1 ignored issue
show
introduced by
The condition $this->getConfigFile() !== null is always true.
Loading history...
97
            $output->writeln(
98
                sprintf('<comment>Config: %s</comment>', $this->getConfigFile()),
99
                OutputInterface::VERBOSITY_VERBOSE
100
            );
101
        }
102
103
        $builder = $this->getBuilder($config);
104
105
        if ((bool) $this->builder->getConfig()->get('cache.enabled')) {
106
            $output->writeln(
107
                sprintf('<comment>Cache: %s</comment>', $this->builder->getConfig()->getCachePath()),
108
                OutputInterface::VERBOSITY_VERBOSE
109
            );
110
        }
111
112
        $builder->build($options);
113
        $this->fs->dumpFile(Util::joinFile($this->getPath(), self::TMP_DIR, 'changes.flag'), time());
114
        $output->writeln('Done! 🎉');
115
116
        return 0;
117
    }
118
}
119