Skip to content

Commit 26cbb7f

Browse files
authored
feat: complete Doctrine ORM state processor example (#1646)
1 parent f9a0b23 commit 26cbb7f

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

core/state-processors.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ services:
8181
#tags: [ 'api_platform.state_processor' ]
8282
```
8383

84-
## Decorating the Built-In State Processors
84+
## Hooking into the Built-In State Processors
8585

86-
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.
86+
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).
8787

8888
The next example uses [Symfony Mailer](https://symfony.com/doc/current/mailer.html). Read its documentation if you want to use it.
8989

@@ -94,25 +94,25 @@ Here is an implementation example which sends new users a welcome email after a
9494

9595
namespace App\State;
9696

97+
use ApiPlatform\Metadata\DeleteOperationInterface;
9798
use ApiPlatform\Metadata\Operation;
9899
use ApiPlatform\State\ProcessorInterface;
99100
use App\Entity\User;
100101
use Symfony\Component\Mailer\MailerInterface;
101102

102103
final class UserProcessor implements ProcessorInterface
103104
{
104-
private $decorated;
105-
private $mailer;
106-
107-
public function __construct(ProcessorInterface $decorated, MailerInterface $mailer)
105+
public function __construct(private ProcessorInterface $persistProcessor, private ProcessorInterface $removeProcessor, MailerInterface $mailer)
108106
{
109-
$this->decorated = $decorated;
110-
$this->mailer = $mailer;
111107
}
112108

113109
public function process($data, Operation $operation, array $uriVariables = [], array $context = [])
114110
{
115-
$result = $this->decorated->process($data, $operation, $uriVariables, $context);
111+
if ($operation instanceof DeleteOperationInterface) {
112+
return $this->removeProcessor($data, $operation, $uriVariables, $context);
113+
}
114+
115+
$result = $this->persistProcessor->process($data, $operation, $uriVariables, $context);
116116
$this->sendWelcomeEmail($data);
117117
return $result;
118118
}
@@ -133,7 +133,8 @@ services:
133133
# ...
134134
App\State\UserProcessor:
135135
bind:
136-
$decorated: '@api_platform.doctrine.orm.state.persist_processor'
136+
$persistProcessor: '@api_platform.doctrine.orm.state.persist_processor'
137+
$removeProcessor: '@api_platform.doctrine.orm.state.remove_processor'
137138
# Uncomment only if autoconfiguration is disabled
138139
#arguments: ['@App\State\UserProcessor.inner']
139140
#tags: [ 'api_platform.state_processor' ]

0 commit comments

Comments
 (0)