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