Skip to content

Commit

Permalink
test: test with soyuka:feat/handle-links-hook
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchalamon committed Aug 10, 2023
1 parent f062fe7 commit b545842
Show file tree
Hide file tree
Showing 16 changed files with 232 additions and 127 deletions.
4 changes: 2 additions & 2 deletions api/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
"repositories": [
{
"type": "vcs",
"url": "git@github.com:vincentchalamon/core.git"
"url": "git@github.com:soyuka/core.git"
}
],
"require": {
"php": ">=8.2",
"ext-ctype": "*",
"ext-iconv": "*",
"ext-xml": "*",
"api-platform/core": "dev-demo",
"api-platform/core": "dev-feat/handle-links-hook",
"doctrine/doctrine-bundle": "^2.7",
"doctrine/doctrine-migrations-bundle": "^3.2",
"doctrine/orm": "^2.12",
Expand Down
16 changes: 8 additions & 8 deletions api/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/config/packages/api_platform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ api_platform:
stateless: true
cache_headers:
vary: ['Content-Type', 'Authorization', 'Origin']
extra_properties:
standard_put: true
oauth:
enabled: true
clientId: '%env(OIDC_SWAGGER_CLIENT_ID)%'
Expand Down
2 changes: 0 additions & 2 deletions api/src/Entity/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
),
],
normalizationContext: [
'item_uri_template' => '/admin/books/{id}{._format}',
'groups' => ['Book:read:admin', 'Enum:read'],
'skip_null_values' => true,
],
Expand All @@ -76,7 +75,6 @@
new Get(),
],
normalizationContext: [
'item_uri_template' => '/books/{id}{._format}',
'groups' => ['Book:read', 'Enum:read'],
'skip_null_values' => true,
]
Expand Down
28 changes: 25 additions & 3 deletions api/src/Entity/Review.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,28 @@

namespace App\Entity;

use ApiPlatform\Doctrine\Orm\State\ItemProvider;
use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\NotExposed;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use ApiPlatform\State\CreateProvider;
use App\Repository\ReviewRepository;
use App\Serializer\IriTransformerNormalizer;
use App\State\Processor\ReviewPersistProcessor;
use App\State\Processor\ReviewRemoveProcessor;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Types\UuidType;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Uid\Uuid;
Expand Down Expand Up @@ -58,7 +64,6 @@
'book' => '/admin/books/{id}{._format}',
'user' => '/admin/users/{id}{._format}',
],
'item_uri_template' => '/admin/reviews/{id}{._format}',
'skip_null_values' => true,
'groups' => ['Review:read', 'Review:read:admin'],
],
Expand Down Expand Up @@ -87,7 +92,9 @@
security: 'is_granted("ROLE_USER")',
// Mercure publish is done manually in MercureProcessor through ReviewPersistProcessor
processor: ReviewPersistProcessor::class,
itemUriTemplate: '/books/{bookId}/reviews/{id}{._format}'
itemUriTemplate: '/books/{bookId}/reviews/{id}{._format}',
provider: ItemProvider::class,
stateOptions: new Options(handleLinks: [Review::class, 'handleLinks'])
),
new Patch(
uriTemplate: '/books/{bookId}/reviews/{id}{._format}',
Expand Down Expand Up @@ -115,7 +122,6 @@
'book' => '/books/{id}{._format}',
'user' => '/users/{id}{._format}',
],
'item_uri_template' => '/books/{bookId}/reviews/{id}{._format}',
'skip_null_values' => true,
'groups' => ['Review:read'],
],
Expand Down Expand Up @@ -193,4 +199,20 @@ public function getId(): ?Uuid
{
return $this->id;
}

public static function handleLinks(
QueryBuilder $queryBuilder,
array $uriVariables,
QueryNameGeneratorInterface $queryNameGenerator,
array $context,
string $entityClass,
Operation $operation
): QueryBuilder {
return $queryBuilder
->resetDQLParts()
->select('b')
->from(Book::class, 'b')
->where('b.id = :id')
->setParameters(['id' => $uriVariables['bookId']]);
}
}
3 changes: 1 addition & 2 deletions api/src/State/Processor/BookPersistProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
}

// save entity
$this->persistProcessor->process($data, $operation, $uriVariables, $context);
$data = $this->persistProcessor->process($data, $operation, $uriVariables, $context);

// publish on Mercure
// todo find a way to do it in API Platform
foreach (['/admin/books/{id}{._format}', '/books/{id}{._format}'] as $uriTemplate) {
$this->mercureProcessor->process(
$data,
Expand Down
5 changes: 2 additions & 3 deletions api/src/State/Processor/BookRemoveProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use ApiPlatform\Api\IriConverterInterface;
use ApiPlatform\Api\UrlGeneratorInterface;
use ApiPlatform\Doctrine\Common\State\RemoveProcessor as DoctrineRemoveProcessor;
use ApiPlatform\Doctrine\Common\State\RemoveProcessor;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\State\ProcessorInterface;
Expand All @@ -16,7 +16,7 @@
final readonly class BookRemoveProcessor implements ProcessorInterface
{
public function __construct(
#[Autowire(service: DoctrineRemoveProcessor::class)]
#[Autowire(service: RemoveProcessor::class)]
private ProcessorInterface $removeProcessor,
#[Autowire(service: MercureProcessor::class)]
private ProcessorInterface $mercureProcessor,
Expand All @@ -36,7 +36,6 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
$this->removeProcessor->process($data, $operation, $uriVariables, $context);

// publish on Mercure
// todo find a way to do it in API Platform
foreach (['/admin/books/{id}{._format}', '/books/{id}{._format}'] as $uriTemplate) {
$iri = $this->iriConverter->getIriFromResource(
$object,
Expand Down
6 changes: 3 additions & 3 deletions api/src/State/Processor/MercureProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
$context['data'] = $this->serializer->serialize(
$data,
key($this->formats),
($operation->getNormalizationContext() ?? [] + [
'item_uri_template' => $context['item_uri_template'] ?? null,
])
($operation->getNormalizationContext() ?? []) + (isset($context['item_uri_template']) ? [
'item_uri_template' => $context['item_uri_template'],
] : [])
);
}

Expand Down
10 changes: 4 additions & 6 deletions api/src/State/Processor/ReviewPersistProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@

namespace App\State\Processor;

use ApiPlatform\Doctrine\Common\State\PersistProcessor;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Entity\Review;
use App\Repository\ReviewRepository;
use Doctrine\Persistence\ObjectRepository;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

final readonly class ReviewPersistProcessor implements ProcessorInterface
{
public function __construct(
#[Autowire(service: ReviewRepository::class)]
private ObjectRepository $repository,
#[Autowire(service: PersistProcessor::class)]
private ProcessorInterface $persistProcessor,
private Security $security,
#[Autowire(service: MercureProcessor::class)]
private ProcessorInterface $mercureProcessor
Expand All @@ -32,10 +31,9 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
$data->publishedAt = new \DateTimeImmutable();

// save entity
$this->repository->save($data, true);
$data = $this->persistProcessor->process($data, $operation, $uriVariables, $context);

// publish on Mercure
// todo find a way to do it in API Platform
foreach (['/admin/reviews/{id}{._format}', '/books/{bookId}/reviews/{id}{._format}'] as $uriTemplate) {
$this->mercureProcessor->process(
$data,
Expand Down
5 changes: 2 additions & 3 deletions api/src/State/Processor/ReviewRemoveProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use ApiPlatform\Api\IriConverterInterface;
use ApiPlatform\Api\UrlGeneratorInterface;
use ApiPlatform\Doctrine\Common\State\RemoveProcessor as DoctrineRemoveProcessor;
use ApiPlatform\Doctrine\Common\State\RemoveProcessor;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\State\ProcessorInterface;
Expand All @@ -16,7 +16,7 @@
final readonly class ReviewRemoveProcessor implements ProcessorInterface
{
public function __construct(
#[Autowire(service: DoctrineRemoveProcessor::class)]
#[Autowire(service: RemoveProcessor::class)]
private ProcessorInterface $removeProcessor,
#[Autowire(service: MercureProcessor::class)]
private ProcessorInterface $mercureProcessor,
Expand All @@ -36,7 +36,6 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
$this->removeProcessor->process($data, $operation, $uriVariables, $context);

// publish on Mercure
// todo find a way to do it in API Platform
foreach (['/admin/reviews/{id}{._format}', '/books/{bookId}/reviews/{id}{._format}'] as $uriTemplate) {
$iri = $this->iriConverter->getIriFromResource(
$object,
Expand Down
Loading

0 comments on commit b545842

Please sign in to comment.