Completed
Push — optimize ( 1cb82b...432add )
by Arnaud
02:20 queued 14s
created

Build   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 25.76 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 8
dl 17
loc 66
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 17 17 1
C execute() 0 39 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 phpDocumentor\Reflection\Types\Boolean;
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
class Build extends Command
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25 View Code Duplication
    protected function configure()
26
    {
27
        $this
28
            ->setName('build')
29
            ->setDescription('Build the website')
30
            ->setDefinition(
31
                new InputDefinition([
32
                    new InputArgument('path', InputArgument::OPTIONAL, 'Use the given path as working directory'),
33
                    new InputOption('drafts', 'd', InputOption::VALUE_NONE, 'Include drafts'),
34
                    new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Build without saving'),
35
                    new InputOption('baseurl', null, InputOption::VALUE_REQUIRED, 'Set the base URL'),
36
                    new InputOption('destination', null, InputOption::VALUE_REQUIRED, 'Set the output directory'),
37
                    new InputOption('optimize', null, InputOption::VALUE_OPTIONAL, 'Optimize output (disable with "no")', false),
38
                ])
39
            )
40
            ->setHelp('Build the website in the output directory.');
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48
        $config = [];
49
        $options = [];
50
        $messageOpt = '';
51
52
        if ($input->getOption('drafts')) {
53
            $options['drafts'] = true;
54
            $messageOpt .= ' with drafts';
55
        }
56
        if ($input->getOption('dry-run')) {
57
            $options['dry-run'] = true;
58
            $messageOpt .= ' dry-run';
59
        }
60
        if ($input->getOption('baseurl')) {
61
            $config['baseurl'] = $input->getOption('baseurl');
62
        }
63
        if ($input->getOption('destination')) {
64
            $config['output']['dir'] = $input->getOption('destination');
65
            $this->fs->dumpFile($this->getPath().'/'.self::TMP_DIR.'/output', $input->getOption('destination'));
66
        }
67
        if ($input->getOption('optimize') === null) {
68
            $config['optimize']['enabled'] = true;
69
        }
70
        if ($input->getOption('optimize') === false || $input->getOption('optimize') == 'no') {
71
            $config['optimize']['enabled'] = false;
72
        }
73
74
        try {
75
            $output->writeln(sprintf('Building website%s...', $messageOpt));
76
            $output->writeln(sprintf('<comment>Path: %s</comment>', $this->getPath()));
77
            $this->getBuilder($output, $config)->build($options);
78
            $this->fs->dumpFile($this->getPath().'/'.self::TMP_DIR.'/changes.flag', time());
79
        } catch (\Exception $e) {
80
            throw new \Exception(sprintf('%s', $e->getMessage()));
81
        }
82
83
        return 0;
84
    }
85
}
86