Passed
Push — master ( f4f5f5...69e5ba )
by Caen
02:58 queued 12s
created

canExistingIndexFileBeOverwritten()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Console\Commands;
6
7
use Hyde\Console\Concerns\AsksToRebuildSite;
8
use Hyde\Framework\Features\Templates\Homepages;
9
use Hyde\Framework\Features\Templates\PublishableContract;
10
use Hyde\Framework\Services\ChecksumService;
11
use Hyde\Hyde;
12
use Illuminate\Support\Collection;
13
use LaravelZero\Framework\Commands\Command;
14
15
/**
16
 * Publish one of the default homepages.
17
 *
18
 * @see \Hyde\Framework\Testing\Feature\Commands\PublishHomepageCommandTest
19
 */
20
class PublishHomepageCommand extends Command
21
{
22
    use AsksToRebuildSite;
23
24
    /** @var string */
25
    protected $signature = 'publish:homepage {homepage? : The name of the page to publish}
26
                                {--force : Overwrite any existing files}';
27
28
    /** @var string */
29
    protected $description = 'Publish one of the default homepages to index.blade.php.';
30
31
    public function handle(): int
32
    {
33
        $selected = $this->parseSelection();
34
35
        if (! Homepages::exists($selected)) {
36
            $this->error("Homepage $selected does not exist.");
37
38
            return 404;
39
        }
40
41
        if (! $this->canExistingFileBeOverwritten()) {
42
            $this->error('A modified index.blade.php file already exists. Use --force to overwrite.');
43
44
            return 409;
45
        }
46
47
        Homepages::get($selected)->publish(true);
48
49
        $this->line("<info>Published page</info> [<comment>$selected</comment>]");
50
51
        $this->askToRebuildSite();
52
53
        return Command::SUCCESS;
54
    }
55
56
    protected function parseSelection(): string
57
    {
58
        return $this->argument('homepage') ?? $this->parseChoiceIntoKey($this->promptForHomepage());
59
    }
60
61
    protected function promptForHomepage(): string
62
    {
63
        return $this->choice(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->choice('Wh...ublishableChoices(), 0) could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
64
            'Which homepage do you want to publish?',
65
            $this->formatPublishableChoices(),
66
            0
67
        );
68
    }
69
70
    protected function formatPublishableChoices(): array
71
    {
72
        return $this->getTemplateOptions()->map(function (array $option, string $key): string {
73
            return  "<comment>$key</comment>: {$option['description']}";
74
        })->values()->toArray();
75
    }
76
77
    protected function getTemplateOptions(): Collection
78
    {
79
        return Homepages::options()->map(fn (PublishableContract $page): array => $page::toArray());
80
    }
81
82
    protected function parseChoiceIntoKey(string $choice): string
83
    {
84
        return strstr(str_replace(['<comment>', '</comment>'], '', $choice), ':', true);
85
    }
86
87
    protected function canExistingFileBeOverwritten(): bool
88
    {
89
        if ($this->option('force')) {
90
            return true;
91
        }
92
93
        if (! file_exists(Hyde::getBladePagePath('index.blade.php'))) {
94
            return true;
95
        }
96
97
        return $this->isTheExistingFileADefaultOne();
98
    }
99
100
    protected function isTheExistingFileADefaultOne(): bool
101
    {
102
        return ChecksumService::checksumMatchesAny(ChecksumService::unixsumFile(
103
            Hyde::getBladePagePath('index.blade.php')
104
        ));
105
    }
106
}
107