105 lines
2.6 KiB
PHP
105 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services\BundleCreators\Creators;
|
|
|
|
use App\Classes\Bundle;
|
|
use App\Exceptions\BundleAlreadyExists;
|
|
use Illuminate\Filesystem\FilesystemAdapter;
|
|
use Illuminate\Support\Str;
|
|
|
|
use function Laravel\Prompts\select;
|
|
use function Laravel\Prompts\text;
|
|
|
|
class CriticBundleCreator extends BaseBundleCreator
|
|
{
|
|
private static string $section = 'critiques';
|
|
|
|
public function __construct(protected ?array $data, protected FilesystemAdapter $disk)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Create a bundle
|
|
*/
|
|
public function createBundle(): string
|
|
{
|
|
$kind = $this->data['kind'];
|
|
$title = $this->data['title'];
|
|
$slug = Str::slug($title);
|
|
$date = now();
|
|
$path = sprintf('%s/%s/%s', static::$section, $kind, $slug);
|
|
$bundle = new Bundle($path, $this->disk);
|
|
|
|
if ($bundle->exists()) {
|
|
throw new BundleAlreadyExists(
|
|
sprintf('A bundle already exists in %s', $path)
|
|
);
|
|
}
|
|
|
|
$bundle->metadata()->setMany([
|
|
'title' => $title,
|
|
'date' => $date->toIso8601String(),
|
|
]);
|
|
|
|
$bundle->markdown()->set('');
|
|
|
|
$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
|
|
!empty($this->data['kind'])
|
|
&& !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['kind'])) {
|
|
$specs['kind'] = fn () => select('Media kind', $this->listKinds());
|
|
}
|
|
|
|
if (empty($this->data['title'])) {
|
|
$specs['title'] = fn () => text('Work title', '', '', true);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
private function listKinds()
|
|
{
|
|
$bundles = Bundle::findBundles($this->disk, static::$section);
|
|
$kinds = [];
|
|
|
|
foreach ($bundles as $bundle) {
|
|
$kinds[basename($bundle->getPath())] = $bundle->metadata()->get('title');
|
|
}
|
|
|
|
asort($kinds, SORT_NATURAL);
|
|
|
|
return $kinds;
|
|
}
|
|
}
|