Passed
Push — master ( 076484...486a30 )
by Caen
05:14 queued 01:57
created

HydePublishHomepageCommand::askToRebuildSite()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Commands;
4
5
use Hyde\Framework\Actions\PublishesHomepageView;
6
use Hyde\Framework\Hyde;
7
use Hyde\Framework\Services\ChecksumService;
8
use Illuminate\Support\Facades\Artisan;
9
use LaravelZero\Framework\Commands\Command;
10
11
/**
12
 * Publish one of the default homepages.
13
 *
14
 * @see \Hyde\Framework\Testing\Feature\Commands\HydePublishHomepageCommandTest
15
 */
16
class HydePublishHomepageCommand extends Command
17
{
18
    /** @var string */
19
    protected $signature = 'publish:homepage {homepage? : The name of the page to publish}
20
                                {--force : Overwrite any existing files}';
21
22
    /** @var string */
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
        if (! $this->canExistingIndexFileBeOverwritten()) {
32
            $this->error('A modified index.blade.php file already exists. Use --force to overwrite.');
33
34
            return 409;
35
        }
36
37
        $returnValue = (new PublishesHomepageView(
38
            $this->selected
39
        ))->execute();
40
41
        if (is_numeric($returnValue)) {
42
            if ($returnValue == 404) {
43
                $this->error('Homepage '.$this->selected.' does not exist.');
44
45
                return 404;
46
            }
47
        }
48
49
        $this->line("<info>Published page</info> [<comment>$this->selected</comment>]");
50
51
        $this->askToRebuildSite();
52
53
        return 0;
54
    }
55
56
    protected function promptForHomepage(): string
57
    {
58
        /** @var string $choice */
59
        $choice = $this->choice(
60
            'Which homepage do you want to publish?',
61
            $this->formatPublishableChoices(),
62
            0
63
        );
64
65
        return $this->parseChoiceIntoKey($choice);
66
    }
67
68
    protected function formatPublishableChoices(): array
69
    {
70
        $keys = [];
71
        foreach (PublishesHomepageView::$homePages as $key => $value) {
72
            $keys[] = "<comment>$key</comment>: {$value['description']}";
73
        }
74
75
        return $keys;
76
    }
77
78
    protected function parseChoiceIntoKey(string $choice): string
79
    {
80
        return strstr(str_replace(['<comment>', '</comment>'], '', $choice), ':', true);
81
    }
82
83
    protected function canExistingIndexFileBeOverwritten(): bool
84
    {
85
        if (! file_exists(Hyde::getBladePagePath('index.blade.php')) || $this->option('force')) {
86
            return true;
87
        }
88
89
        return ChecksumService::checksumMatchesAny(ChecksumService::unixsumFile(
90
            Hyde::getBladePagePath('index.blade.php')
91
        )) || $this->option('force');
92
    }
93
94
    protected function askToRebuildSite(): void
95
    {
96
        if ($this->option('no-interaction')) {
97
            return;
98
        }
99
100
        if ($this->confirm('Would you like to rebuild the site?', 'Yes')) {
0 ignored issues
show
Bug introduced by
'Yes' of type string is incompatible with the type boolean expected by parameter $default of Illuminate\Console\Command::confirm(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

100
        if ($this->confirm('Would you like to rebuild the site?', /** @scrutinizer ignore-type */ 'Yes')) {
Loading history...
101
            $this->line('Okay, building site!');
102
            Artisan::call('build');
103
            $this->info('Site is built!');
104
        } else {
105
            $this->line('Okay, you can always run the build later!');
106
        }
107
    }
108
}
109