| Conditions | 13 |
| Paths | 718 |
| Total Lines | 96 |
| Code Lines | 64 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | } |
||
| 151 |