Skip to content

feat: Complete Doctrine ORM state processor example #1646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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