Introduce typed DTOs and sub-namespaces for Micropub handlers
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>
This commit is contained in:
parent
24cb6d61aa
commit
ad15b36f4d
12 changed files with 282 additions and 36 deletions
51
app/Services/Micropub/Handlers/EntryHandler.php
Normal file
51
app/Services/Micropub/Handlers/EntryHandler.php
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
<?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,
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue