Skip to content

Commit

Permalink
chore: update api-platform/core to 3.3.3 (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchalamon authored May 13, 2024
1 parent cdfef83 commit 9720364
Show file tree
Hide file tree
Showing 26 changed files with 75 additions and 585 deletions.
1 change: 0 additions & 1 deletion api/.php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
'phpdoc_to_comment' => false,
'void_return' => true,
'concat_space' => ['spacing' => 'one'],
'braces' => ['allow_single_line_closure' => true],
'nullable_type_declaration' => true,
])
->setFinder($finder)
Expand Down
2 changes: 1 addition & 1 deletion api/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"php": ">=8.3",
"ext-ctype": "*",
"ext-iconv": "*",
"api-platform/core": "^3.3.0@beta",
"api-platform/core": "^3.3",
"doctrine/common": "^3.4",
"doctrine/doctrine-bundle": "^2.11",
"doctrine/doctrine-fixtures-bundle": "^3.5",
Expand Down
20 changes: 9 additions & 11 deletions api/composer.lock

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

2 changes: 1 addition & 1 deletion api/config/packages/api_platform.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
api_platform:
title: API Platform's demo
version: 3.3.0
version: 3.3.3
description: |
This is a demo application of the [API Platform](https://api-platform.com) framework.
[Its source code](https://github.com/api-platform/demo) includes various examples, check it out!
Expand Down
3 changes: 1 addition & 2 deletions api/src/Doctrine/Orm/Filter/NameFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use ApiPlatform\Doctrine\Orm\Filter\AbstractFilter;
use ApiPlatform\Doctrine\Orm\PropertyHelperTrait;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Exception\InvalidArgumentException;
use ApiPlatform\Metadata\Operation;
use Doctrine\ORM\QueryBuilder;

Expand Down Expand Up @@ -73,7 +72,7 @@ protected function normalizeValues($value, string $property): ?array

if (empty($values)) {
$this->getLogger()->notice('Invalid filter ignored', [
'exception' => new InvalidArgumentException(sprintf('At least one value is required, multiple values should be in "%1$s[]=firstvalue&%1$s[]=secondvalue" format', $property)),
'exception' => new \InvalidArgumentException(sprintf('At least one value is required, multiple values should be in "%1$s[]=firstvalue&%1$s[]=secondvalue" format', $property)),
]);

return null;
Expand Down
27 changes: 21 additions & 6 deletions api/src/Entity/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\UrlGeneratorInterface;
use App\Enum\BookCondition;
use App\Repository\BookRepository;
use App\State\Processor\BookPersistProcessor;
use App\State\Processor\BookRemoveProcessor;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
Expand All @@ -44,7 +47,6 @@
paginationClientItemsPerPage: true
),
new Post(
// Mercure publish is done manually in MercureProcessor through BookPersistProcessor
processor: BookPersistProcessor::class,
itemUriTemplate: '/admin/books/{id}{._format}'
),
Expand All @@ -54,12 +56,10 @@
// https://github.com/api-platform/admin/issues/370
new Put(
uriTemplate: '/admin/books/{id}{._format}',
// Mercure publish is done manually in MercureProcessor through BookPersistProcessor
processor: BookPersistProcessor::class
),
new Delete(
uriTemplate: '/admin/books/{id}{._format}',
// Mercure publish is done manually in MercureProcessor through BookRemoveProcessor
processor: BookRemoveProcessor::class
),
],
Expand All @@ -71,7 +71,13 @@
AbstractNormalizer::GROUPS => ['Book:write'],
],
collectDenormalizationErrors: true,
security: 'is_granted("OIDC_ADMIN")'
security: 'is_granted("OIDC_ADMIN")',
mercure: [
'topics' => [
'@=iri(object, ' . UrlGeneratorInterface::ABS_URL . ', get_operation(object, "/admin/books/{id}{._format}"))',
'@=iri(object, ' . UrlGeneratorInterface::ABS_URL . ', get_operation(object, "/books/{id}{._format}"))',
],
]
)]
#[ApiResource(
types: ['https://schema.org/Book', 'https://schema.org/Offer'],
Expand Down Expand Up @@ -155,14 +161,18 @@ class Book
/**
* An IRI of reviews.
*
* @var Collection<int, Review>
*
* @see https://schema.org/reviews
*/
#[ApiProperty(
types: ['https://schema.org/reviews'],
example: '/books/6acacc80-8321-4d83-9b02-7f2c7bf6eb1d/reviews'
example: '/books/6acacc80-8321-4d83-9b02-7f2c7bf6eb1d/reviews',
uriTemplate: '/books/{bookId}/reviews{._format}'
)]
#[Groups(groups: ['Book:read', 'Bookmark:read'])]
public ?string $reviews = null;
#[ORM\OneToMany(targetEntity: Review::class, mappedBy: 'book')]
public Collection $reviews;

/**
* The overall rating, based on a collection of reviews or ratings, of the item.
Expand All @@ -176,6 +186,11 @@ class Book
#[Groups(groups: ['Book:read', 'Book:read:admin', 'Bookmark:read'])]
public ?int $rating = null;

public function __construct()
{
$this->reviews = new ArrayCollection();
}

public function getId(): ?Uuid
{
return $this->id;
Expand Down
16 changes: 9 additions & 7 deletions api/src/Entity/Review.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\UrlGeneratorInterface;
use ApiPlatform\State\CreateProvider;
use App\Repository\ReviewRepository;
use App\Security\Voter\OidcTokenPermissionVoter;
Expand Down Expand Up @@ -56,12 +57,10 @@
// https://github.com/api-platform/admin/issues/370
new Put(
uriTemplate: '/admin/reviews/{id}{._format}',
// Mercure publish is done manually in MercureProcessor through ReviewPersistProcessor
processor: ReviewPersistProcessor::class
),
new Delete(
uriTemplate: '/admin/reviews/{id}{._format}',
// Mercure publish is done manually in MercureProcessor through ReviewRemoveProcessor
processor: ReviewRemoveProcessor::class
),
],
Expand All @@ -77,7 +76,13 @@
AbstractNormalizer::GROUPS => ['Review:write', 'Review:write:admin'],
],
collectDenormalizationErrors: true,
security: 'is_granted("OIDC_ADMIN")'
security: 'is_granted("OIDC_ADMIN")',
mercure: [
'topics' => [
'@=iri(object, ' . UrlGeneratorInterface::ABS_URL . ', get_operation(object, "/admin/reviews/{id}{._format}"))',
'@=iri(object, ' . UrlGeneratorInterface::ABS_URL . ', get_operation(object, "/books/{bookId}/reviews/{id}{._format}"))',
],
]
)]
#[ApiResource(
types: ['https://schema.org/Review'],
Expand All @@ -100,7 +105,6 @@
),
new Post(
security: 'is_granted("OIDC_USER")',
// Mercure publish is done manually in MercureProcessor through ReviewPersistProcessor
processor: ReviewPersistProcessor::class,
provider: CreateProvider::class,
itemUriTemplate: '/books/{bookId}/reviews/{id}{._format}',
Expand All @@ -114,7 +118,6 @@
],
/** @see OidcTokenPermissionVoter */
security: 'is_granted("OIDC_USER", request.getRequestUri())',
// Mercure publish is done manually in MercureProcessor through ReviewPersistProcessor
processor: ReviewPersistProcessor::class
),
new Delete(
Expand All @@ -125,7 +128,6 @@
],
/** @see OidcTokenPermissionVoter */
security: 'is_granted("OIDC_USER", request.getRequestUri())',
// Mercure publish is done manually in MercureProcessor through ReviewRemoveProcessor
processor: ReviewRemoveProcessor::class
),
],
Expand Down Expand Up @@ -172,7 +174,7 @@ class Review
#[ApiProperty(types: ['https://schema.org/itemReviewed'])]
#[Assert\NotNull]
#[Groups(groups: ['Review:read', 'Review:write:admin'])]
#[ORM\ManyToOne(targetEntity: Book::class)]
#[ORM\ManyToOne(targetEntity: Book::class, inversedBy: 'reviews')]
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
public ?Book $book = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace App\Security\Http\Protection;

use ApiPlatform\Api\UrlGeneratorInterface;
use ApiPlatform\Metadata\IriConverterInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\UrlGeneratorInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand Down
5 changes: 0 additions & 5 deletions api/src/Serializer/BookNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use App\Repository\ReviewRepository;
use Doctrine\Persistence\ObjectRepository;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
Expand All @@ -21,7 +20,6 @@ final class BookNormalizer implements NormalizerInterface, NormalizerAwareInterf
* @param ReviewRepository $repository
*/
public function __construct(
private RouterInterface $router,
#[Autowire(service: ReviewRepository::class)]
private ObjectRepository $repository
) {
Expand All @@ -32,9 +30,6 @@ public function __construct(
*/
public function normalize(mixed $object, ?string $format = null, array $context = []): array
{
$object->reviews = $this->router->generate('_api_/books/{bookId}/reviews{._format}_get_collection', [
'bookId' => $object->getId(),
]);
$object->rating = $this->repository->getAverageRating($object);

return $this->normalizer->normalize($object, $format, [self::class => true] + $context);
Expand Down
4 changes: 2 additions & 2 deletions api/src/Serializer/IriTransformerNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace App\Serializer;

use ApiPlatform\Api\IriConverterInterface;
use ApiPlatform\Api\UrlGeneratorInterface;
use ApiPlatform\JsonLd\Serializer\ItemNormalizer;
use ApiPlatform\Metadata\IriConverterInterface;
use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface;
use ApiPlatform\Metadata\UrlGeneratorInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
Expand Down
19 changes: 1 addition & 18 deletions api/src/State/Processor/BookPersistProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@
{
/**
* @param PersistProcessor $persistProcessor
* @param MercureProcessor $mercureProcessor
*/
public function __construct(
#[Autowire(service: PersistProcessor::class)]
private ProcessorInterface $persistProcessor,
#[Autowire(service: MercureProcessor::class)]
private ProcessorInterface $mercureProcessor,
private HttpClientInterface $client,
private DecoderInterface $decoder
) {
Expand All @@ -49,21 +46,7 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
}

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

// publish on Mercure
foreach (['/admin/books/{id}{._format}', '/books/{id}{._format}'] as $uriTemplate) {
$this->mercureProcessor->process(
$data,
$operation,
$uriVariables,
$context + [
'item_uri_template' => $uriTemplate,
]
);
}

return $data;
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
}

private function getData(string $uri): array
Expand Down
30 changes: 1 addition & 29 deletions api/src/State/Processor/BookRemoveProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@

namespace App\State\Processor;

use ApiPlatform\Api\IriConverterInterface;
use ApiPlatform\Api\UrlGeneratorInterface;
use ApiPlatform\Doctrine\Common\State\RemoveProcessor;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\State\ProcessorInterface;
use App\Entity\Book;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
Expand All @@ -19,16 +16,11 @@
final readonly class BookRemoveProcessor implements ProcessorInterface
{
/**
* @param RemoveProcessor $removeProcessor
* @param MercureProcessor $mercureProcessor
* @param RemoveProcessor $removeProcessor
*/
public function __construct(
#[Autowire(service: RemoveProcessor::class)]
private ProcessorInterface $removeProcessor,
#[Autowire(service: MercureProcessor::class)]
private ProcessorInterface $mercureProcessor,
private ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory,
private IriConverterInterface $iriConverter
) {
}

Expand All @@ -37,27 +29,7 @@ public function __construct(
*/
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): void
{
$object = clone $data;

// remove entity
$this->removeProcessor->process($data, $operation, $uriVariables, $context);

// publish on Mercure
foreach (['/admin/books/{id}{._format}', '/books/{id}{._format}'] as $uriTemplate) {
$iri = $this->iriConverter->getIriFromResource(
$object,
UrlGeneratorInterface::ABS_URL,
$this->resourceMetadataCollectionFactory->create(Book::class)->getOperation($uriTemplate)
);
$this->mercureProcessor->process(
$object,
$operation,
$uriVariables,
$context + [
'item_uri_template' => $uriTemplate,
MercureProcessor::DATA => json_encode(['@id' => $iri]),
]
);
}
}
}
Loading

0 comments on commit 9720364

Please sign in to comment.