| Total Complexity | 52 |
| Total Lines | 309 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AbstractCommand often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class AbstractCommand extends Command |
||
| 38 | { |
||
| 39 | public const CONFIG_FILE = ['cecil.yml', 'config.yml']; |
||
| 40 | public const TMP_DIR = '.cecil'; |
||
| 41 | public const EXCLUDED_CMD = ['about', 'new:site', 'self-update']; |
||
| 42 | public const SERVE_OUTPUT = '.cecil/preview'; |
||
| 43 | |||
| 44 | /** @var InputInterface */ |
||
| 45 | protected $input; |
||
| 46 | |||
| 47 | /** @var OutputInterface */ |
||
| 48 | protected $output; |
||
| 49 | |||
| 50 | /** @var SymfonyStyle */ |
||
| 51 | protected $io; |
||
| 52 | |||
| 53 | /** @var string */ |
||
| 54 | protected $rootPath; |
||
| 55 | |||
| 56 | /** @var null|string */ |
||
| 57 | private $path = null; |
||
| 58 | |||
| 59 | /** @var array */ |
||
| 60 | private $configFiles = []; |
||
| 61 | |||
| 62 | /** @var Config */ |
||
| 63 | private $config; |
||
| 64 | |||
| 65 | /** @var Builder */ |
||
| 66 | private $builder; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritdoc} |
||
| 70 | */ |
||
| 71 | protected function initialize(InputInterface $input, OutputInterface $output) |
||
| 72 | { |
||
| 73 | $this->input = $input; |
||
| 74 | $this->output = $output; |
||
| 75 | $this->io = new SymfonyStyle($input, $output); |
||
| 76 | $this->rootPath = (Util\Platform::isPhar() ? Util\Platform::getPharPath() : realpath(Util::joinFile(__DIR__, '/../../'))) . '/'; |
||
| 77 | |||
| 78 | // prepare configuration files list |
||
| 79 | if (!\in_array($this->getName(), self::EXCLUDED_CMD)) { |
||
| 80 | // site config file |
||
| 81 | $this->configFiles[$this->locateConfigFile($this->getPath())['name']] = $this->locateConfigFile($this->getPath())['path']; |
||
| 82 | // additional config file(s) from --config=<file> |
||
| 83 | if ($input->hasOption('config') && $input->getOption('config') !== null) { |
||
| 84 | $this->configFiles += $this->locateAdditionalConfigFiles($this->getPath(), (string) $input->getOption('config')); |
||
| 85 | } |
||
| 86 | // checks file(s) |
||
| 87 | $this->configFiles = array_unique($this->configFiles); |
||
| 88 | foreach ($this->configFiles as $fileName => $filePath) { |
||
| 89 | if ($filePath === false) { |
||
| 90 | unset($this->configFiles[$fileName]); |
||
| 91 | $this->io->warning(\sprintf('Could not find configuration file "%s".', $fileName)); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | parent::initialize($input, $output); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * {@inheritdoc} |
||
| 101 | */ |
||
| 102 | public function run(InputInterface $input, OutputInterface $output): int |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Send desktop notification. |
||
| 149 | */ |
||
| 150 | public function notification(string $body, ?string $url = null): void |
||
| 151 | { |
||
| 152 | $notifier = new DefaultNotifier(); |
||
| 153 | $notification = (new Notification()) |
||
| 154 | ->setTitle('Cecil') |
||
| 155 | ->setIcon($this->rootPath . 'resources/icon.png') |
||
| 156 | ->setBody($body) |
||
| 157 | ; |
||
| 158 | if ($url !== null) { |
||
| 159 | $notification->addOption('url', $url); |
||
| 160 | } |
||
| 161 | if (false === $notifier->send($notification)) { |
||
| 162 | $this->output->writeln('<comment>Desktop notification could not be sent</comment>'); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Returns the working path. |
||
| 168 | */ |
||
| 169 | protected function getPath(bool $exist = true): ?string |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Returns config file(s) path. |
||
| 198 | */ |
||
| 199 | protected function getConfigFiles(): array |
||
| 200 | { |
||
| 201 | return $this->configFiles ?? []; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Creates or returns a Builder instance. |
||
| 206 | * |
||
| 207 | * @throws RuntimeException |
||
| 208 | */ |
||
| 209 | protected function getBuilder(array $config = []): Builder |
||
| 210 | { |
||
| 211 | try { |
||
| 212 | // loads configuration files if not already done |
||
| 213 | if ($this->config === null) { |
||
| 214 | $this->config = new Config(); |
||
| 215 | // loads and merges configuration files |
||
| 216 | foreach ($this->getConfigFiles() as $filePath) { |
||
| 217 | $this->config->import($this->config::loadFile($filePath), Config::IMPORT_MERGE); |
||
| 218 | } |
||
| 219 | // merges configuration from $config parameter |
||
| 220 | $this->config->import($config, Config::IMPORT_MERGE); |
||
| 221 | } |
||
| 222 | // creates builder instance if not already done |
||
| 223 | if ($this->builder === null) { |
||
| 224 | $this->builder = (new Builder($this->config, new ConsoleLogger($this->output))) |
||
| 225 | ->setSourceDir($this->getPath()) |
||
| 226 | ->setDestinationDir($this->getPath()); |
||
| 227 | } |
||
| 228 | } catch (\Exception $e) { |
||
| 229 | throw new RuntimeException($e->getMessage()); |
||
| 230 | } |
||
| 231 | |||
| 232 | return $this->builder; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Locates the configuration in the given path, as an array of the file name and path, if file exists, otherwise default name and false. |
||
| 237 | */ |
||
| 238 | protected function locateConfigFile(string $path): array |
||
| 239 | { |
||
| 240 | $config = [ |
||
| 241 | 'name' => self::CONFIG_FILE[0], |
||
| 242 | 'path' => false, |
||
| 243 | ]; |
||
| 244 | foreach (self::CONFIG_FILE as $configFileName) { |
||
| 245 | if (($configFilePath = realpath(Util::joinFile($path, $configFileName))) !== false) { |
||
| 246 | $config = [ |
||
| 247 | 'name' => $configFileName, |
||
| 248 | 'path' => $configFilePath, |
||
| 249 | ]; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | return $config; |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Locates additional configuration file(s) from the given list of files, relative to the given path or absolute. |
||
| 258 | */ |
||
| 259 | protected function locateAdditionalConfigFiles(string $path, string $configFilesList): array |
||
| 260 | { |
||
| 261 | $config = []; |
||
| 262 | foreach (explode(',', $configFilesList) as $filename) { |
||
| 263 | // absolute path |
||
| 264 | $config[$filename] = realpath($filename); |
||
| 265 | // relative path |
||
| 266 | if (!Util\File::getFS()->isAbsolutePath($filename)) { |
||
| 267 | $config[$filename] = realpath(Util::joinFile($path, $filename)); |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | return $config; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Opens path with editor. |
||
| 276 | * |
||
| 277 | * @throws RuntimeException |
||
| 278 | */ |
||
| 279 | protected function openEditor(string $path, string $editor): void |
||
| 280 | { |
||
| 281 | $command = \sprintf('%s "%s"', $editor, $path); |
||
| 282 | switch (Util\Platform::getOS()) { |
||
| 283 | case Util\Platform::OS_WIN: |
||
| 284 | $command = \sprintf('start /B "" %s "%s"', $editor, $path); |
||
| 285 | break; |
||
| 286 | case Util\Platform::OS_OSX: |
||
| 287 | // Typora on macOS |
||
| 288 | if ($editor == 'typora') { |
||
| 289 | $command = \sprintf('open -a typora "%s"', $path); |
||
| 290 | } |
||
| 291 | break; |
||
| 292 | } |
||
| 293 | $process = Process::fromShellCommandline($command); |
||
| 294 | $process->run(); |
||
| 295 | if (!$process->isSuccessful()) { |
||
| 296 | throw new RuntimeException(\sprintf('Unable to use "%s" editor.', $editor)); |
||
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Validate URL. |
||
| 302 | * |
||
| 303 | * @throws RuntimeException |
||
| 304 | */ |
||
| 305 | public static function validateUrl(string $url): string |
||
| 306 | { |
||
| 307 | if ($url == '/') { // tolerate root URL |
||
| 308 | return $url; |
||
| 309 | } |
||
| 310 | $validator = Validation::createValidator(); |
||
| 311 | $violations = $validator->validate($url, new Url()); |
||
| 312 | if (\count($violations) > 0) { |
||
| 313 | foreach ($violations as $violation) { |
||
| 314 | throw new RuntimeException($violation->getMessage()); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | return rtrim($url, '/') . '/'; |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Returns the "binary name" in the console context. |
||
| 322 | */ |
||
| 323 | protected function binName(): string |
||
| 324 | { |
||
| 325 | return basename($_SERVER['argv'][0]); |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Override default help message. |
||
| 330 | * |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | public function getProcessedHelp(): string |
||
| 346 | } |
||
| 347 | } |
||
| 348 |