Files

50 lines
1.4 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Console\Commands;
use App\Models\Post;
use App\Support\Publishing\WordPressPublisher;
use Illuminate\Console\Command;
class PublishPost extends Command
{
protected $signature = 'posts:publish {post}';
protected $description = 'Publish an existing blog post to its site\'s WordPress REST API';
public function handle(WordPressPublisher $publisher): int
{
$post = Post::with('site')->find($this->argument('post'));
if (! $post) {
return $this->failWith("Post {$this->argument('post')} not found.");
}
if ($post->type !== 'blog') {
return $this->failWith("Post {$post->id} has type '{$post->type}', expected 'blog' to publish to a CMS.");
}
$result = $publisher->publish($post);
if ($result->success) {
$post->update(['status' => 'published', 'published_url' => $result->url, 'publish_error' => null]);
$this->line(json_encode(['success' => true, 'url' => $result->url]));
return self::SUCCESS;
}
if ($result->attempted) {
$post->update(['status' => 'failed', 'publish_error' => $result->error]);
}
return $this->failWith($result->error);
}
private function failWith(string $message): int
{
$this->line(json_encode(['success' => false, 'error' => $message]));
return self::FAILURE;
}
}