1
0
cms11/app/Services/BundleCreator/Creators/BlogBundleCreator.php

96 lines
2.5 KiB
PHP
Raw Normal View History

2024-04-17 11:41:10 +02:00
<?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()->setMany([
2024-04-17 11:41:10 +02:00
'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;
}
}