Skip to content

Commit

Permalink
feat: complete Doctrine ORM state processor example (api-platform#1646)
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm authored Nov 9, 2022
1 parent f9a0b23 commit 26cbb7f
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions core/state-processors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -94,25 +94,25 @@ 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;
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;
}
Expand All @@ -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' ]
Expand Down

0 comments on commit 26cbb7f

Please sign in to comment.