Each handler now declares the data class it needs via dataClass(). The controller builds the appropriate typed DTO from the raw request array before calling handle(), giving handlers typed property access instead of raw array lookups. Handlers moved to App\Services\Micropub\Handlers, data objects to App\Services\Micropub\Data. MicropubHandlerRegistry and the interface are documented with flow diagrams and guidance for adding new types. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
51 lines
1.4 KiB
PHP
51 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Micropub\Handlers;
|
|
|
|
use App\Exceptions\InvalidTokenScopeException;
|
|
use App\Services\ArticleService;
|
|
use App\Services\BookmarkService;
|
|
use App\Services\LikeService;
|
|
use App\Services\Micropub\Data\EntryData;
|
|
use App\Services\Micropub\Data\MicropubData;
|
|
use App\Services\NoteService;
|
|
|
|
class EntryHandler implements MicropubHandlerInterface
|
|
{
|
|
public function dataClass(): string
|
|
{
|
|
return EntryData::class;
|
|
}
|
|
|
|
/**
|
|
* @throws InvalidTokenScopeException
|
|
*/
|
|
public function handle(MicropubData $data): array
|
|
{
|
|
assert($data instanceof EntryData);
|
|
|
|
$scopes = $data->tokenData['scope'];
|
|
if (is_string($scopes)) {
|
|
$scopes = explode(' ', $scopes);
|
|
}
|
|
|
|
if (! in_array('create', $scopes, true)) {
|
|
throw new InvalidTokenScopeException;
|
|
}
|
|
|
|
$dataArray = $data->toArray();
|
|
$location = match (true) {
|
|
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,
|
|
};
|
|
|
|
return [
|
|
'response' => 'created',
|
|
'url' => $location,
|
|
];
|
|
}
|
|
}
|