| Conditions | 12 |
| Paths | 186 |
| Total Lines | 85 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 71 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 72 | { |
||
| 73 | $force = $input->getOption('force'); |
||
| 74 | $demo = $input->getOption('demo'); |
||
| 75 | |||
| 76 | try { |
||
| 77 | // ask to override existing site? |
||
| 78 | if (Util\File::getFS()->exists(Util::joinFile($this->getPath(false), $this->locateConfigFile($this->getPath())['name'] ?: self::CONFIG_FILE[0])) && !$force) { |
||
| 79 | $output->writeln('<comment>Website already exists.</comment>'); |
||
| 80 | if (!$this->io->confirm('Do you want to override it?', false)) { |
||
| 81 | return 0; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | // setup questions |
||
| 85 | $title = $this->io->ask('Give a title to your website', 'New website'); |
||
| 86 | $baseline = $this->io->ask('Give a baseline to your website', ''); |
||
| 87 | $baseurl = $this->io->ask('Base URL?', '/', [$this, 'validateUrl']); |
||
| 88 | $description = $this->io->ask('Write a full description of your site', 'Website created with Cecil.'); |
||
| 89 | $demo = ($demo !== false) ?: $this->io->confirm('Add demo content?', false); |
||
| 90 | // override skeleton default config |
||
| 91 | $config = Yaml::parseFile(Util::joinPath($this->rootPath, 'resources/skeleton', self::CONFIG_FILE[0]), Yaml::PARSE_DATETIME); |
||
| 92 | $config = array_replace_recursive($config, [ |
||
| 93 | 'title' => $title, |
||
| 94 | 'baseline' => $baseline, |
||
| 95 | 'baseurl' => $baseurl, |
||
| 96 | 'description' => $description |
||
| 97 | ]); |
||
| 98 | $configYaml = Yaml::dump($config, 2, 2); |
||
| 99 | Util\File::getFS()->dumpFile(Util::joinPath($this->getPath(), $this->locateConfigFile($this->getPath())['name'] ?: self::CONFIG_FILE[0]), $configYaml); |
||
| 100 | // create path dir |
||
| 101 | Util\File::getFS()->mkdir($this->getPath(false)); |
||
| 102 | // creates sub dir |
||
| 103 | foreach ( |
||
| 104 | [ |
||
| 105 | (string) $this->getBuilder()->getConfig()->get('assets.dir'), |
||
| 106 | (string) $this->getBuilder()->getConfig()->get('layouts.dir'), |
||
| 107 | (string) $this->getBuilder()->getConfig()->get('pages.dir'), |
||
| 108 | (string) $this->getBuilder()->getConfig()->get('static.dir'), |
||
| 109 | ] as $value |
||
| 110 | ) { |
||
| 111 | Util\File::getFS()->mkdir(Util::joinPath($this->getPath(), $value)); |
||
| 112 | } |
||
| 113 | // copy files |
||
| 114 | foreach ( |
||
| 115 | [ |
||
| 116 | 'assets/favicon.png', |
||
| 117 | 'assets/icon.png', |
||
| 118 | 'pages/index.md', |
||
| 119 | ] as $value |
||
| 120 | ) { |
||
| 121 | Util\File::getFS()->copy( |
||
| 122 | Util::joinPath($this->rootPath, 'resources/skeleton', $value), |
||
| 123 | Util::joinPath($this->getPath(), $value) |
||
| 124 | ); |
||
| 125 | } |
||
| 126 | // demo: copy all files |
||
| 127 | if ($demo) { |
||
| 128 | foreach ( |
||
| 129 | [ |
||
| 130 | (string) $this->getBuilder()->getConfig()->get('assets.dir'), |
||
| 131 | (string) $this->getBuilder()->getConfig()->get('layouts.dir'), |
||
| 132 | (string) $this->getBuilder()->getConfig()->get('pages.dir'), |
||
| 133 | (string) $this->getBuilder()->getConfig()->get('static.dir'), |
||
| 134 | ] as $value |
||
| 135 | ) { |
||
| 136 | Util\File::getFS()->mirror( |
||
| 137 | Util::joinPath($this->rootPath, 'resources/skeleton', $value), |
||
| 138 | Util::joinPath($this->getPath(), $value) |
||
| 139 | ); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | // done |
||
| 143 | $output->writeln(\sprintf('<info>Your new website is created in %s.</info>', realpath($this->getPath()))); |
||
| 144 | $this->io->newLine(); |
||
| 145 | $this->io->listing([ |
||
| 146 | 'Start the built-in preview server with <info>' . $this->binName() . ' serve</info>', |
||
| 147 | 'You can create a new page with <info>' . $this->binName() . ' new:page</info>', |
||
| 148 | ]); |
||
| 149 | |||
| 150 | $this->io->text('Visit <href=https://cecil.app>https://cecil.app</> for documentation and more.'); |
||
| 151 | } catch (\Exception $e) { |
||
| 152 | throw new RuntimeException(\sprintf($e->getMessage())); |
||
| 153 | } |
||
| 154 | |||
| 155 | return 0; |
||
| 156 | } |
||
| 158 |