96 lines
2.5 KiB
PHP
96 lines
2.5 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Services\BundleCreator\Creators;
|
||
|
|
||
|
use App\Classes\Bundle;
|
||
|
use App\Exceptions\BundleAlreadyExists;
|
||
|
use Illuminate\Filesystem\FilesystemAdapter;
|
||
|
use Illuminate\Support\Str;
|
||
|
|
||
|
use function Laravel\Prompts\confirm;
|
||
|
use function Laravel\Prompts\text;
|
||
|
|
||
|
class BlogBundleCreator extends BaseBundleCreator
|
||
|
{
|
||
|
private static string $section = 'blog';
|
||
|
|
||
|
public function __construct(protected ?array $data, protected FilesystemAdapter $disk)
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Create a bundle
|
||
|
*/
|
||
|
public function createBundle(): string
|
||
|
{
|
||
|
$title = $this->data['title'];
|
||
|
$date = $this->data['publish_today'] ? now() : now()->addDay();
|
||
|
$path = sprintf('%s/%s/%s', static::$section, $date->format('Y/m/d'), Str::slug($title));
|
||
|
|
||
|
$bundle = new Bundle($path, $this->disk);
|
||
|
|
||
|
if ($bundle->exists()) {
|
||
|
throw new BundleAlreadyExists(
|
||
|
sprintf('A bundle already exists in %s', $path)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
$bundle->markdown()->set('');
|
||
|
$bundle->metadata()->set([
|
||
|
'title' => $title,
|
||
|
'date' => $date->toIso8601String(),
|
||
|
]);
|
||
|
|
||
|
$bundle->save();
|
||
|
|
||
|
return $path;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Return a boolean value indicating if the creator can actually make the
|
||
|
* bundle using known data.
|
||
|
*/
|
||
|
public function canCreateBundle(): bool
|
||
|
{
|
||
|
return array_key_exists('publish_today', $this->data)
|
||
|
&& !empty($this->data['title']);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Return an array describing what kind of data the creator needs in
|
||
|
* addition to the one it already has
|
||
|
*/
|
||
|
public function formSpecs(): ?array
|
||
|
{
|
||
|
$specs = [];
|
||
|
|
||
|
if (empty($this->data['title'])) {
|
||
|
$specs['title'] = fn () => text('Article title', '', '', true);
|
||
|
}
|
||
|
|
||
|
$today = now()->format('d/m/Y');
|
||
|
$tomorrow = now()->addDay()->format('d/m/Y');
|
||
|
|
||
|
if (!array_key_exists('publish_today', $this->data)) {
|
||
|
$specs['publish_today'] = fn () => confirm(
|
||
|
'Do you want to publish today?',
|
||
|
true,
|
||
|
sprintf('%s (%s)', 'Publish today', $today),
|
||
|
sprintf('%s (%s)', 'Publish tomorrow', $tomorrow)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
return $specs;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Return a boolean value indicating if this creator in particular can
|
||
|
* create bundles for specified section
|
||
|
*/
|
||
|
public static function handles(string $section, ?array $data = []): bool
|
||
|
{
|
||
|
return $section === static::$section;
|
||
|
}
|
||
|
}
|