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

99 lines
2.4 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;
2024-04-17 15:29:28 +02:00
use App\Services\Browser;
2024-04-17 11:41:10 +02:00
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
{
2024-04-17 15:29:28 +02:00
$url = $this->data['url'];
$hash = md5($url);
$date = now();
$path = sprintf('%s/%s/%s', static::$section, $date->format('Y/m/d'), $hash);
2024-04-17 11:41:10 +02:00
$bundle = new Bundle($path, $this->disk);
if ($bundle->exists()) {
throw new BundleAlreadyExists(
sprintf('A bundle already exists in %s', $path)
);
}
2024-04-17 15:29:28 +02:00
$browser = new Browser($url);
$browser->go();
$screenshot = $browser->getScreenshot();
$title = $browser->getTitle();
$description = $browser->getDescription();
$ref = $bundle->attachments('images')->addToHistory('screenshot', [
'contents' => $screenshot,
'filename' => 'screenshot.jpg',
]);
$bundle->metadata()->set([
'title' => $title,
'date' => $date->toIso8601String(),
'cover' => $ref,
]);
$bundle->markdown()->set('> ' . $description . "\n\n");
2024-04-17 11:41:10 +02:00
$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;
}
}