From 26cbb7fd190b09db360b288558b66a7181f3ee4f Mon Sep 17 00:00:00 2001 From: Martin Herndl Date: Wed, 9 Nov 2022 12:20:04 +0100 Subject: [PATCH] feat: complete Doctrine ORM state processor example (#1646) --- core/state-processors.md | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/core/state-processors.md b/core/state-processors.md index b0da9760d27..30ddf8a6124 100644 --- a/core/state-processors.md +++ b/core/state-processors.md @@ -81,9 +81,9 @@ services: #tags: [ 'api_platform.state_processor' ] ``` -## Decorating the Built-In State Processors +## Hooking into the Built-In State Processors -If you want to execute custom business logic before or after persistence, this can be achieved by [decorating](https://symfony.com/doc/current/service_container/service_decoration.html) the built-in state processors. +If you want to execute custom business logic before or after persistence, this can be achieved by [decorating](https://symfony.com/doc/current/service_container/service_decoration.html) the built-in state processors or using [composition](https://en.wikipedia.org/wiki/Object_composition). The next example uses [Symfony Mailer](https://symfony.com/doc/current/mailer.html). Read its documentation if you want to use it. @@ -94,6 +94,7 @@ Here is an implementation example which sends new users a welcome email after a namespace App\State; +use ApiPlatform\Metadata\DeleteOperationInterface; use ApiPlatform\Metadata\Operation; use ApiPlatform\State\ProcessorInterface; use App\Entity\User; @@ -101,18 +102,17 @@ use Symfony\Component\Mailer\MailerInterface; final class UserProcessor implements ProcessorInterface { - private $decorated; - private $mailer; - - public function __construct(ProcessorInterface $decorated, MailerInterface $mailer) + public function __construct(private ProcessorInterface $persistProcessor, private ProcessorInterface $removeProcessor, MailerInterface $mailer) { - $this->decorated = $decorated; - $this->mailer = $mailer; } public function process($data, Operation $operation, array $uriVariables = [], array $context = []) { - $result = $this->decorated->process($data, $operation, $uriVariables, $context); + if ($operation instanceof DeleteOperationInterface) { + return $this->removeProcessor($data, $operation, $uriVariables, $context); + } + + $result = $this->persistProcessor->process($data, $operation, $uriVariables, $context); $this->sendWelcomeEmail($data); return $result; } @@ -133,7 +133,8 @@ services: # ... App\State\UserProcessor: bind: - $decorated: '@api_platform.doctrine.orm.state.persist_processor' + $persistProcessor: '@api_platform.doctrine.orm.state.persist_processor' + $removeProcessor: '@api_platform.doctrine.orm.state.remove_processor' # Uncomment only if autoconfiguration is disabled #arguments: ['@App\State\UserProcessor.inner'] #tags: [ 'api_platform.state_processor' ]