Lines
72.72%
32 / 44
Methods
66.66%
2 / 3
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| configure | 100.00% | 11 / 11 | 100.00% | 1 / 1 | 1 | ||
| execute | 62.50% | 20 / 32 | 0.00% | 0 / 1 | 21.91 | ||
| 34 | class CreateHashesCommand extends Command | |
| 35 | { | |
| 36 | public function __construct( | |
| 37 | private readonly System $system, | |
| 38 | private readonly Filesystem $filesystem, | |
| 39 | ) { | |
| 40 | parent::__construct(); | |
| 41 | } | |
| 42 | ||
| 43 | protected function configure(): void | |
| 44 | { | |
| 45 | $this->addOption( | |
| 46 | name: 'root', | |
| 47 | shortcut: null, | |
| 48 | mode: InputOption::VALUE_OPTIONAL, | |
| 49 | description: 'Optional phpMyFAQ root directory if different from the current install', | |
| 50 | )->addOption( | |
| 51 | name: 'out', | |
| 52 | shortcut: null, | |
| 53 | mode: InputOption::VALUE_OPTIONAL, | |
| 54 | description: 'Optional file to store the generated hashes as JSON', | |
| 55 | ); | |
| 56 | } | |
| 57 | ||
| 58 | protected function execute(InputInterface $input, OutputInterface $output): int | |
| 59 | { | |
| 60 | $symfonyStyle = new SymfonyStyle($input, $output); | |
| 61 | ||
| 62 | $root = $input->getOption('root'); | |
| 63 | $rootDir = defined('PMF_ROOT_DIR') ? PMF_ROOT_DIR : null; | |
| 64 | if (is_string($root) && '' !== $root) { | |
| 65 | if (!is_dir($root)) { | |
| 66 | $symfonyStyle->error(sprintf("The provided root directory '%s' does not exist.", $root)); | |
| 67 | return Command::FAILURE; | |
| 68 | } | |
| 69 | ||
| 70 | $rootDir = rtrim($root, characters: '/'); | |
| 71 | } | |
| 72 | ||
| 73 | if (null !== $rootDir && !defined('PMF_ROOT_DIR')) { | |
| 74 | define('PMF_ROOT_DIR', $rootDir); | |
| 75 | } | |
| 76 | ||
| 77 | if (!defined('PMF_ROOT_DIR')) { | |
| 78 | $symfonyStyle->error( | |
| 79 | 'PMF_ROOT_DIR is not defined. Provide --root option or run inside a configured installation.', | |
| 80 | ); | |
| 81 | return Command::FAILURE; | |
| 82 | } | |
| 83 | ||
| 84 | try { | |
| 85 | $hashes = $this->system->createHashes(); | |
| 86 | } catch (Throwable $throwable) { | |
| 87 | $symfonyStyle->error('Failed to create hashes: ' . $throwable->getMessage()); | |
| 88 | return Command::FAILURE; | |
| 89 | } | |
| 90 | ||
| 91 | $outputPath = $input->getOption('out'); | |
| 92 | if (is_string($outputPath) && '' !== $outputPath) { | |
| 93 | $directory = dirname($outputPath); | |
| 94 | if (!$this->filesystem->exists($directory)) { | |
| 95 | $this->filesystem->mkdir($directory); | |
| 96 | } | |
| 97 | ||
| 98 | try { | |
| 99 | $this->filesystem->dumpFile($outputPath, $hashes); | |
| 100 | } catch (Throwable $throwable) { | |
| 101 | $symfonyStyle->error('Unable to write hashes: ' . $throwable->getMessage()); | |
| 102 | return Command::FAILURE; | |
| 103 | } | |
| 104 | ||
| 105 | $symfonyStyle->success(sprintf('Hashes written to %s', $outputPath)); | |
| 106 | return Command::SUCCESS; | |
| 107 | } | |
| 108 | ||
| 109 | $output->writeln($hashes); | |
| 110 | return Command::SUCCESS; | |
| 111 | } | |
| 112 | } |