-
-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce GutendexBookRepository
- Loading branch information
1 parent
07b6dcf
commit 86e9930
Showing
14 changed files
with
186 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
api/src/BookRepository/Exception/UnsupportedBookException.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\BookRepository; | ||
|
||
use App\Entity\Book; | ||
use Symfony\Component\Serializer\Encoder\DecoderInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
final readonly class GutendexBookRepository implements RestrictedBookRepositoryInterface | ||
{ | ||
public function __construct( | ||
private HttpClientInterface $gutendexClient, | ||
private DecoderInterface $decoder, | ||
) { | ||
} | ||
|
||
public function supports(string $url): bool | ||
{ | ||
return str_starts_with($url, 'https://gutendex.com'); | ||
} | ||
|
||
public function find(string $url): ?Book | ||
{ | ||
$options = ['headers' => ['Accept' => 'application/json']]; | ||
$response = $this->gutendexClient->request('GET', $url, $options); | ||
if (200 !== $response->getStatusCode()) { | ||
return null; | ||
} | ||
|
||
$book = new Book(); | ||
|
||
$data = $this->decoder->decode($response->getContent(), 'json'); | ||
$book->title = $data['title']; | ||
$book->author = $data['authors'][0]['name'] ?? null; | ||
|
||
return $book; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Validator; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
#[\Attribute(\Attribute::TARGET_PROPERTY)] | ||
final class BookUrl extends Constraint | ||
{ | ||
public string $message = 'This book URL is not valid.'; | ||
|
||
public function __construct(?array $options = null, ?string $message = null, ?array $groups = null, mixed $payload = null) | ||
{ | ||
parent::__construct($options ?? [], $groups, $payload); | ||
|
||
$this->message = $message ?? $this->message; | ||
} | ||
|
||
public function getTargets(): string | ||
{ | ||
return self::PROPERTY_CONSTRAINT; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Validator; | ||
|
||
use App\BookRepository\BookRepositoryInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
|
||
final class BookUrlValidator extends ConstraintValidator | ||
{ | ||
public function __construct( | ||
private readonly BookRepositoryInterface $bookRepository, | ||
) { | ||
} | ||
|
||
/** | ||
* @param string|null $value | ||
*/ | ||
public function validate($value, Constraint $constraint): void | ||
{ | ||
if (!$constraint instanceof BookUrl) { | ||
throw new UnexpectedTypeException($constraint, BookUrl::class); | ||
} | ||
|
||
if (null === $value || '' === $value) { | ||
return; | ||
} | ||
|
||
if (!$this->bookRepository->find($value)) { | ||
$this->context->buildViolation($constraint->message)->addViolation(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { SearchDoc } from "./SearchDoc"; | ||
|
||
export class Search { | ||
constructor( | ||
public results: Array<SearchDoc>, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export class SearchDoc { | ||
constructor( | ||
public id?: number, | ||
public title?: string, | ||
public authors?: Array<string>, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters