84 lines
2.0 KiB
PHP
84 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services\BundleCreator\Creators;
|
|
|
|
use App\Classes\Bundle;
|
|
use App\Exceptions\BundleAlreadyExists;
|
|
use Illuminate\Filesystem\FilesystemAdapter;
|
|
|
|
use function Laravel\Prompts\text;
|
|
|
|
class LinkBundleCreator extends BaseBundleCreator
|
|
{
|
|
private static string $section = 'liens-interessants';
|
|
|
|
public function __construct(protected ?array $data, protected FilesystemAdapter $disk)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Create a bundle
|
|
*/
|
|
public function createBundle(): string
|
|
{
|
|
$url = $this->data['url'];
|
|
$hash = md5($url);
|
|
$date = now();
|
|
$path = sprintf('%s/%s/%s', static::$section, $date->format('Y/m/d'), $hash);
|
|
|
|
$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 !empty($this->data['url']);
|
|
}
|
|
|
|
/**
|
|
* 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['url'])) {
|
|
$specs['url'] = fn () => text('Interesting link URL', '', '', true, [
|
|
'url' => 'url',
|
|
]);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|