Custom processors are useful to customize your logic to send an email, persist data to storage, add to queue and for an advanced usage such as an hexagonal architecture.
Example #1: Sending an email after persisting data
As an example, send an email after customer registration
namespace App\Sylius\State\Processor;
use Sylius\Component\Customer\Model\CustomerInterface;
use Sylius\Resource\Doctrine\Common\State\PersistProcessor;
use Sylius\Resource\State\ProcessorInterface;
final class CreateCustomerProcessor implements ProcessorInterface
{
public function __construct(
private CommandBusInterface $commandBus,
private PersistProcessor $decorated,
) {
}
public function process(mixed $data, Operation $operation, Context $context): mixed
{
Assert::isInstanceOf($data, Customer::class);
$this->decorated->process($data, $operation, $context);
// Here your logic to send a registration email.
$this->commandBus->dispatch(new SendRegistrationEmailCommand(new CustomerId($data->id)));
return null;
}
}
Use this processor on your operation.
src/Entity/Customer.php
namespace App\Entity\Customer;
use App\Sylius\State\Processor\CreateCustomerProcessor;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Metadata\Create;
use Sylius\Resource\Model\ResourceInterface;
#[AsResource(
operations: [
new Create(
processor: CreateCustomerProcessor::class,
),
],
)
final class BoardGameResource implements ResourceInterface
Example #2: Use a custom delete processor
As another example, let's configure a DeleteBoardGameProcessor on a BoardGameResource which is not a Doctrine entity.
namespace App\BoardGameBlog\Infrastructure\Sylius\Resource;
use App\BoardGameBlog\Infrastructure\Sylius\State\Http\Provider\BoardGameItemProvider;
use App\BoardGameBlog\Infrastructure\Symfony\Form\Type\BoardGameType;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Metadata\Update;
use Sylius\Resource\Model\ResourceInterface;
#[AsResource(
section: 'admin',
formType: BoardGameType::class,
templatesDir: 'crud',
routePrefix: '/admin',
operations: [
new Update(
shortName: 'update_preview',
provider: BoardGameItemProvider::class,
write: false,
),
],
)]
final class BoardGameResource implements ResourceInterface
Note that in a delete operation, you can disable providing data.
See chapter.