2025-04-27 16:38:25 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-02-22 10:41:33 +00:00
|
|
|
namespace App\Services\Micropub\Handlers;
|
2025-04-27 16:38:25 +01:00
|
|
|
|
|
|
|
|
use App\Exceptions\InvalidTokenScopeException;
|
|
|
|
|
use App\Services\ArticleService;
|
|
|
|
|
use App\Services\BookmarkService;
|
|
|
|
|
use App\Services\LikeService;
|
2026-02-22 10:41:33 +00:00
|
|
|
use App\Services\Micropub\Data\EntryData;
|
|
|
|
|
use App\Services\Micropub\Data\MicropubData;
|
2025-04-27 16:38:25 +01:00
|
|
|
use App\Services\NoteService;
|
|
|
|
|
|
|
|
|
|
class EntryHandler implements MicropubHandlerInterface
|
|
|
|
|
{
|
2026-02-22 10:41:33 +00:00
|
|
|
public function dataClass(): string
|
|
|
|
|
{
|
|
|
|
|
return EntryData::class;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-27 16:38:25 +01:00
|
|
|
/**
|
|
|
|
|
* @throws InvalidTokenScopeException
|
|
|
|
|
*/
|
2026-02-22 10:41:33 +00:00
|
|
|
public function handle(MicropubData $data): array
|
2025-04-27 16:38:25 +01:00
|
|
|
{
|
2026-02-22 10:41:33 +00:00
|
|
|
assert($data instanceof EntryData);
|
|
|
|
|
|
|
|
|
|
$scopes = $data->tokenData['scope'];
|
2025-04-27 16:38:25 +01:00
|
|
|
if (is_string($scopes)) {
|
|
|
|
|
$scopes = explode(' ', $scopes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! in_array('create', $scopes, true)) {
|
|
|
|
|
throw new InvalidTokenScopeException;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-22 10:41:33 +00:00
|
|
|
$dataArray = $data->toArray();
|
2025-04-27 16:38:25 +01:00
|
|
|
$location = match (true) {
|
2026-02-22 10:41:33 +00:00
|
|
|
isset($dataArray['like-of']) => resolve(LikeService::class)->create($dataArray)->url,
|
|
|
|
|
isset($dataArray['bookmark-of']) => resolve(BookmarkService::class)->create($dataArray)->uri,
|
|
|
|
|
isset($dataArray['name']) => resolve(ArticleService::class)->create($dataArray)->link,
|
|
|
|
|
default => resolve(NoteService::class)->create($dataArray)->uri,
|
2025-04-27 16:38:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'response' => 'created',
|
|
|
|
|
'url' => $location,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|