Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up the MakePostCommand class #640

Merged
merged 10 commits into from
Nov 3, 2022
56 changes: 38 additions & 18 deletions packages/framework/src/Console/Commands/MakePostCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class MakePostCommand extends Command
{
/** @var string */
protected $signature = 'make:post
{title? : The title for the Post. Will be used to generate the slug}
{--force : Should the generated file overwrite existing posts with the same slug?}';
{title? : The title for the Post. Will also be used to generate the filename}
{--force : Should the generated file overwrite existing posts with the same filename?}';

/** @var string */
protected $description = 'Scaffold a new Markdown blog post file';
Expand All @@ -26,40 +26,59 @@ public function handle(): int
{
$this->title('Creating a new post!');

$this->line(
$this->argument('title')
$title = $this->getTitle();

[$description, $author, $category] = $this->getSelections();

$creator = new CreatesNewMarkdownPostFile($title, $description, $category, $author);

$this->displaySelections($creator);

if (! $this->confirm('Do you wish to continue?', true)) {
$this->info('Aborting.');

return 130;
}

return $this->createPostFile($creator);
}

protected function getTitle(): mixed
{
$this->line($this->argument('title')
? '<info>Selected title: '.$this->argument('title')."</info>\n"
: 'Please enter the title of the post, it will be used to generate the slug.'
: 'Please enter the title of the post, it will be used to generate the filename.'
);

$title = $this->argument('title')
return $this->argument('title')
?? $this->ask('What is the title of the post?')
?? 'My New Post';
}

protected function getSelections(): array
{
$this->line('Tip: You can just hit return to use the defaults.');

$description = $this->ask('Write a short post excerpt/description');
$author = $this->ask('What is your (the author\'s) name?');
$category = $this->ask('What is the primary category of the post?');

return [$description, $author, $category];
}

protected function displaySelections(CreatesNewMarkdownPostFile $creator): void
{
$this->info('Creating a post with the following details:');
$creator = new CreatesNewMarkdownPostFile(
$title,
$description,
$category,
$author
);

foreach ($creator->toArray() as $key => $value) {
$this->line(sprintf('%s: %s', ucwords($key), $value));
}
$this->line("Identifier: {$creator->getIdentifier()}");

if (! $this->confirm('Do you wish to continue?', true)) {
$this->info('Aborting.');

return 130;
}
$this->line("Identifier: {$creator->getIdentifier()}");
}

protected function createPostFile(CreatesNewMarkdownPostFile $creator): int
{
try {
$path = $creator->save($this->option('force'));
$this->info("Post created! File is saved to $path");
Expand All @@ -68,6 +87,7 @@ public function handle(): int
} catch (Exception $exception) {
$this->error('Something went wrong when trying to save the file!');
$this->warn($exception->getMessage());

if ($exception->getCode() === 409) {
$this->comment('If you want to overwrite the file supply the --force flag.');
}
Expand Down