Passed
Pull Request — cli (#1896)
by Arnaud
09:48 queued 05:48
created

NewSite::execute()   F

Complexity

Conditions 13
Paths 718

Size

Total Lines 96
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 13
eloc 64
c 4
b 0
f 0
nc 718
nop 2
dl 0
loc 96
rs 3.0587

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Command;
15
16
use Cecil\Exception\RuntimeException;
17
use Cecil\Util;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputDefinition;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
use Symfony\Component\Console\Output\OutputInterface;
23
use Symfony\Component\Yaml\Yaml;
24
25
/**
26
 * Creates a new website.
27
 */
28
class NewSite extends AbstractCommand
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected function configure()
34
    {
35
        $this
36
            ->setName('new:site')
37
            ->setDescription('Creates a new website')
38
            ->setDefinition(
39
                new InputDefinition([
40
                    new InputArgument('path', InputArgument::OPTIONAL, 'Use the given path as working directory'),
41
                    new InputOption('force', 'f', InputOption::VALUE_NONE, 'Override directory if it already exists'),
42
                    new InputOption('demo', null, InputOption::VALUE_NONE, 'Add demo content (pages, templates and assets)'),
43
                ])
44
            )
45
            ->setHelp('Creates a new website in the current directory, or in <path> if provided');
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     *
51
     * @throws RuntimeException
52
     */
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        $force = $input->getOption('force');
56
        $demo = $input->getOption('demo');
57
58
        try {
59
            // ask to override existing site?
60
            if (Util\File::getFS()->exists(Util::joinFile($this->getPath(), $this->findConfigFile('name') ?: self::CONFIG_FILE[0])) && !$force) {
61
                $output->writeln('<comment>Website already exists.</comment>');
62
                if (!$this->io->confirm('Do you want to override it?', false)) {
63
                    return 0;
64
                }
65
            }
66
            // define root path
67
            $root = realpath(Util::joinFile(__DIR__, '/../../'));
68
            if (Util\Plateform::isPhar()) {
69
                $root = Util\Plateform::getPharPath() . '/';
70
            }
71
            // setup questions
72
            $title = $this->io->ask('Give a title to your new site', 'Site title');
73
            $baseline = $this->io->ask('Describe your site in few words', '');
74
            $baseurl = $this->io->ask('Base URL?', 'https://cecil.app/', [$this, 'validateUrl']);
75
            $description = $this->io->ask('Write a full description of your site', 'Site description.');
76
            $authorName = $this->io->ask('What is the author name?', 'Cecil');
77
            $authorUrl = $this->io->ask('What is the author URL?', 'https://cecil.app', [$this, 'validateUrl']);
78
            if ($this->io->confirm('Add demo content?', false)) {
79
                $demo = true;
80
            }
81
            // override skeleton default config
82
            $config = Yaml::parseFile(Util::joinPath($root, 'resources/skeleton', self::CONFIG_FILE[0]));
83
            $config = array_replace_recursive($config, [
84
                'title'       => $title,
85
                'baseline'    => $baseline,
86
                'baseurl'     => $baseurl,
87
                'description' => $description,
88
                'author'      => [
89
                    'name' => $authorName,
90
                    'url'  => $authorUrl,
91
                ],
92
            ]);
93
            $configYaml = Yaml::dump($config, 2, 2);
94
            Util\File::getFS()->dumpFile(Util::joinPath($this->getPath(), $this->findConfigFile('name') ?: self::CONFIG_FILE[0]), $configYaml);
95
            // creates dir
96
            foreach (
97
                [
98
                    (string) $this->getBuilder()->getConfig()->get('assets.dir'),
99
                    (string) $this->getBuilder()->getConfig()->get('layouts.dir'),
100
                    (string) $this->getBuilder()->getConfig()->get('pages.dir'),
101
                    (string) $this->getBuilder()->getConfig()->get('static.dir'),
102
                ] as $value
103
            ) {
104
                Util\File::getFS()->mkdir(Util::joinPath($this->getPath(), $value));
105
            }
106
            // copy files
107
            foreach (
108
                [
109
                    'assets/favicon.png',
110
                    'pages/index.md',
111
                    'static/cecil-card.png',
112
                ] as $value
113
            ) {
114
                Util\File::getFS()->copy(
115
                    Util::joinPath($root, 'resources/skeleton', $value),
116
                    Util::joinPath($this->getPath(), $value)
117
                );
118
            }
119
            // demo: copy files
120
            if ($demo) {
121
                foreach (
122
                    [
123
                        (string) $this->getBuilder()->getConfig()->get('assets.dir'),
124
                        (string) $this->getBuilder()->getConfig()->get('layouts.dir'),
125
                        (string) $this->getBuilder()->getConfig()->get('pages.dir'),
126
                        (string) $this->getBuilder()->getConfig()->get('static.dir'),
127
                    ] as $value
128
                ) {
129
                    Util\File::getFS()->mirror(
130
                        Util::joinPath($root, 'resources/skeleton', $value),
131
                        Util::joinPath($this->getPath(), $value)
132
                    );
133
                }
134
            }
135
            // done
136
            $output->writeln(sprintf('<info>Your new Cecil site is created in %s.</info>', realpath($this->getPath())));
137
            $this->io->newLine();
138
            $this->io->listing([
139
                'You can download a theme from https://cecil.app/themes/',
140
                'You can create a new page with "cecil new:page"',
141
                'Start the built-in preview server via "cecil serve"',
142
            ]);
143
            $this->io->text('Visit https://cecil.app for full documentation.');
144
        } catch (\Exception $e) {
145
            throw new RuntimeException(sprintf($e->getMessage()));
146
        }
147
148
        return 0;
149
    }
150
}
151