Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
32 / 32 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| HydePublishHomepageCommand | |
100.00% |
32 / 32 |
|
100.00% |
5 / 5 |
12 | |
100.00% |
1 / 1 |
| handle | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
5 | |||
| promptForHomepage | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| formatPublishableChoices | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| parseChoiceIntoKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| canExistingIndexFileBeOverwritten | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Hyde\Framework\Commands; |
| 4 | |
| 5 | use Hyde\Framework\Actions\PublishesHomepageView; |
| 6 | use Hyde\Framework\Concerns\Commands\AsksToRebuildSite; |
| 7 | use Hyde\Framework\Hyde; |
| 8 | use Hyde\Framework\Services\FileCacheService; |
| 9 | use LaravelZero\Framework\Commands\Command; |
| 10 | |
| 11 | /** |
| 12 | * Publish one of the default homepages. |
| 13 | * |
| 14 | * @see \Tests\Feature\Commands\HydePublishHomepageCommandTest |
| 15 | */ |
| 16 | class HydePublishHomepageCommand extends Command |
| 17 | { |
| 18 | use AsksToRebuildSite; |
| 19 | |
| 20 | protected $signature = 'publish:homepage {homepage? : The name of the page to publish} |
| 21 | {--force : Overwrite any existing files}'; |
| 22 | |
| 23 | protected $description = 'Publish one of the default homepages to index.blade.php.'; |
| 24 | |
| 25 | protected string $selected; |
| 26 | |
| 27 | public function handle(): int |
| 28 | { |
| 29 | $this->selected = $this->argument('homepage') ?? $this->promptForHomepage(); |
| 30 | |
| 31 | $returnValue = (new PublishesHomepageView( |
| 32 | $this->selected, |
| 33 | $this->canExistingIndexFileBeOverwritten() |
| 34 | ))->execute(); |
| 35 | |
| 36 | // @deprecated version 0.10.0, can be removed as it should not be possible to select a homepage that does not exist, and we can make a pre-check for 409 case. |
| 37 | if ($returnValue === true) { |
| 38 | $this->info('Homepage published successfully!'); |
| 39 | } else { |
| 40 | if (is_numeric($returnValue)) { |
| 41 | if ($returnValue == 404) { |
| 42 | $this->error('Homepage '.$this->selected.' does not exist.'); |
| 43 | |
| 44 | return 404; |
| 45 | } |
| 46 | |
| 47 | if ($returnValue == 409) { |
| 48 | $this->error('A modified index.blade.php file already exists. Use --force to overwrite.'); |
| 49 | |
| 50 | return 409; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | $this->askToRebuildSite(); |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | protected function promptForHomepage(): string |
| 61 | { |
| 62 | $choice = $this->choice( |
| 63 | 'Which homepage do you want to publish?', |
| 64 | $this->formatPublishableChoices(), |
| 65 | 0 |
| 66 | ); |
| 67 | |
| 68 | $choice = $this->parseChoiceIntoKey($choice); |
| 69 | |
| 70 | $this->line("<info>Selected page</info> [<comment>$choice</comment>]"); |
| 71 | $this->newLine(); |
| 72 | |
| 73 | return $choice; |
| 74 | } |
| 75 | |
| 76 | protected function formatPublishableChoices(): array |
| 77 | { |
| 78 | $keys = []; |
| 79 | foreach (PublishesHomepageView::$homePages as $key => $value) { |
| 80 | $keys[] = "<comment>$key</comment>: {$value['description']}"; |
| 81 | } |
| 82 | |
| 83 | return $keys; |
| 84 | } |
| 85 | |
| 86 | protected function parseChoiceIntoKey(string $choice): string |
| 87 | { |
| 88 | return strstr(str_replace(['<comment>', '</comment>'], '', $choice), ':', true); |
| 89 | } |
| 90 | |
| 91 | protected function canExistingIndexFileBeOverwritten(): bool |
| 92 | { |
| 93 | if (! file_exists(Hyde::getBladePagePath('index.blade.php')) || $this->option('force')) { |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | return FileCacheService::checksumMatchesAny(FileCacheService::unixsumFile( |
| 98 | Hyde::getBladePagePath('index.blade.php') |
| 99 | )) ?? $this->option('force'); |
| 100 | } |
| 101 | } |