Completed
Push — symfony-console ( 505007...6e7652 )
by Arnaud
02:41 queued 10s
created

NewSite::execute()   B

Complexity

Conditions 6
Paths 37

Size

Total Lines 28

Duplication

Lines 7
Ratio 25 %

Importance

Changes 0
Metric Value
dl 7
loc 28
rs 8.8497
c 0
b 0
f 0
cc 6
nc 37
nop 2
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Command;
10
11
use Cecil\Util\Plateform;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputDefinition;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Question\ConfirmationQuestion;
18
19
class NewSite extends Command
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected function configure()
25
    {
26
        $this
27
            ->setName('new:site')
28
            ->setDescription('Create a new website')
29
            ->setDefinition(
30
                new InputDefinition([
31
                    new InputArgument('path', InputArgument::OPTIONAL, 'If specified, use the given path as working directory'),
32
                    new InputOption('force', 'f', InputOption::VALUE_NONE, 'Override the directory if already exist'),
33
                ])
34
            )
35
            ->setHelp('Create a new website in the current directory, or in <path> if provided.');
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $this->force = $input->getOption('force');
0 ignored issues
show
Bug introduced by
The property force 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...
44
45
        try {
46 View Code Duplication
            if ($this->fs->exists($this->getPath().'/'.self::CONFIG_FILE) && !$this->force) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
                $helper = $this->getHelper('question');
48
                $question = new ConfirmationQuestion('Website already exists. Do you want to override it? [y/n]', false);
49
                if (!$helper->ask($input, $output, $question)) {
50
                    return;
51
                }
52
            }
53
            $root = __DIR__.'/../../';
54
            if (Plateform::isPhar()) {
55
                $root = Plateform::getPharPath().'/';
56
            }
57
            $output->writeln('<info>Creating a new website...</info>');
58
            $this->fs->copy($root.'res/skeleton/config.yml', $this->getPath().'/'.self::CONFIG_FILE, true);
59
            $this->fs->mirror($root.'res/skeleton/content', $this->getPath().'/content');
60
            $this->fs->mirror($root.'res/skeleton/layouts', $this->getPath().'/layouts');
61
            $this->fs->mirror($root.'res/skeleton/static', $this->getPath().'/static');
62
            $output->writeln('Done!');
63
        } catch (\Exception $e) {
64
            throw new \Exception(sprintf($e->getMessage()));
65
        }
66
67
        return 0;
68
    }
69
}
70