1
0
cms11/app/Services/BundleCreators/Creators/CriticBundleCreator.php

149 lines
4.5 KiB
PHP
Raw Normal View History

2024-04-17 11:41:10 +02:00
<?php
namespace App\Services\BundleCreators\Creators;
2024-04-17 11:41:10 +02:00
2024-04-18 00:43:53 +02:00
use App\Classes\Bundle;
2024-04-19 11:21:33 +02:00
use App\Exceptions\BundleAlreadyExists;
use App\Services\Wikidata\WikidataClient;
use App\Services\Wikidata\WikidataExtractor;
use Exception;
2024-04-17 11:41:10 +02:00
use Illuminate\Filesystem\FilesystemAdapter;
2024-04-19 11:21:33 +02:00
use Illuminate\Support\Str;
2024-04-17 11:41:10 +02:00
2024-04-19 11:21:33 +02:00
use function Laravel\Prompts\confirm;
2024-04-17 11:41:10 +02:00
use function Laravel\Prompts\select;
use function Laravel\Prompts\text;
class CriticBundleCreator extends BaseBundleCreator
{
2024-04-18 00:43:53 +02:00
private static string $section = 'critiques';
2024-04-17 11:41:10 +02:00
public function __construct(protected ?array $data, protected FilesystemAdapter $disk)
{
//
}
2024-04-19 11:21:33 +02:00
/**
* Create a bundle
*/
public function createBundle(): string
{
$entityId = $this->data['entity_id'];
if ($entityId === false) {
throw new Exception('Bundle creation cancelled');
}
$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)
);
}
$wikidata = app()->make(WikidataClient::class);
$extractor = app()->make(WikidataExtractor::class);
$completeEntity = $wikidata->getEntityData($entityId, true)['entities'][$entityId];
$extractor->extract($completeEntity, $entityId);
$bundle->metadata('wikidata/included')->setMany($extractor->included());
$bundle->metadata('wikidata/excluded')->setMany($extractor->excluded());
$bundle->metadata('wikidata/unused')->setMany($extractor->unused());
$bundle->metadata('wikidata/entity')->setMany($extractor->everythingElse());
$frenchTitle = $bundle->metadata('wikidata/entity')->get('labels.fr.value', null);
$originalTitle = $bundle->metadata('wikidata/entity')->get('labels.en.value', null);
if (!empty($originalTitle)) {
$bundle->metadata()->set('title', $originalTitle);
if (!empty($frenchTitle) && $frenchTitle !== $originalTitle) {
$bundle->metadata()->set('subTitle', $frenchTitle);
}
}
$bundle->metadata()->setMany([
'date' => $date->toIso8601String(),
]);
$bundle->markdown()->set('');
$bundle->save();
return $path;
}
2024-04-17 11:41:10 +02:00
/**
* 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'])
2024-04-19 11:21:33 +02:00
&& array_key_exists('entity_id', $this->data);
2024-04-17 11:41:10 +02:00
}
/**
* 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);
}
2024-04-19 11:21:33 +02:00
if (!empty($this->data['kind']) && !empty($this->data['title']) && empty($this->data['entity_id'])) {
$options = app()->make(WikidataClient::class)->searchEntityId($this->data['title']);
if (!empty($options)) {
$specs['entity_id'] = fn () => select('Confirm searched work', $options);
} else {
$specs['entity_id'] = fn () => confirm(
'No entityId was found in Wikidata for this work. Do you want to create the bundle anyway?'
);
}
2024-04-17 11:41:10 +02:00
}
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;
}
2024-04-17 11:41:10 +02:00
private function listKinds()
{
2024-04-18 00:43:53 +02:00
$bundles = Bundle::findBundles($this->disk, static::$section);
$kinds = [];
foreach ($bundles as $bundle) {
$kinds[basename($bundle->getPath())] = $bundle->metadata()->get('title');
}
asort($kinds, SORT_NATURAL);
2024-04-17 11:41:10 +02:00
return $kinds;
}
}