|
| 1 | +# Basic operations |
| 2 | + |
| 3 | +## Book creation |
| 4 | + |
| 5 | +<div data-full-width="false"> |
| 6 | + |
| 7 | +<figure><img src="../../.gitbook/assets/create_book_resource.png" alt="Create book resource"></figure> |
| 8 | + |
| 9 | +</div> |
| 10 | + |
| 11 | +In the Application folder, we already have this "CreateBookCommand": |
| 12 | + |
| 13 | +```php |
| 14 | +// src/Bookstore/Application/Command/CreateBookCommand.php |
| 15 | +declare(strict_types=1); |
| 16 | + |
| 17 | +namespace App\BookStore\Application\Command; |
| 18 | + |
| 19 | +use App\BookStore\Domain\Model\Book; |
| 20 | +use App\BookStore\Domain\ValueObject\Author; |
| 21 | +use App\BookStore\Domain\ValueObject\BookContent; |
| 22 | +use App\BookStore\Domain\ValueObject\BookDescription; |
| 23 | +use App\BookStore\Domain\ValueObject\BookName; |
| 24 | +use App\BookStore\Domain\ValueObject\Price; |
| 25 | +use App\Shared\Application\Command\CommandInterface; |
| 26 | + |
| 27 | +/** |
| 28 | + * @implements CommandInterface<Book> |
| 29 | + */ |
| 30 | +final readonly class CreateBookCommand implements CommandInterface |
| 31 | +{ |
| 32 | + public function __construct( |
| 33 | + public BookName $name, |
| 34 | + public BookDescription $description, |
| 35 | + public Author $author, |
| 36 | + public BookContent $content, |
| 37 | + public Price $price, |
| 38 | + ) { |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +The idea is to reuse this command to create the book in the storage for your "create" operation. |
| 44 | + |
| 45 | +```php |
| 46 | +// src/BookStore/Infrastructure/Sylius/Resource/BookResource.php |
| 47 | +// ... |
| 48 | +use App\BookStore\Infrastructure\Sylius\State\Processor\CreateBookProcessor; |
| 49 | +use App\BookStore\Infrastructure\Symfony\Form\BookResourceType; |
| 50 | +use Sylius\Resource\Metadata\Create; |
| 51 | +// ... |
| 52 | + |
| 53 | +#[AsResource( |
| 54 | + // ... |
| 55 | + formType: BookResourceType::class, // Define the form type for all your operations |
| 56 | + operations: [ |
| 57 | + new Create( |
| 58 | + processor: CreateBookProcessor::class, |
| 59 | + // formType: CreateBookResourceType::class, // Optional: define a specific form type only for the "create" operation |
| 60 | + ), |
| 61 | + ], |
| 62 | +)] |
| 63 | +final class BookResource implements ResourceInterface |
| 64 | +{ |
| 65 | + // ... |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +Now you need to write your CreateBookProcessor. |
| 70 | + |
| 71 | +```php |
| 72 | +// src/BookStore/Infrastructure/Sylius/State/Processor/CreateBookProcessor.php |
| 73 | + |
| 74 | +namespace App\BookStore\Infrastructure\Sylius\State\Processor; |
| 75 | + |
| 76 | +use App\BookStore\Application\Command\CreateBookCommand; |
| 77 | +use App\BookStore\Domain\ValueObject\Author; |
| 78 | +use App\BookStore\Domain\ValueObject\BookContent; |
| 79 | +use App\BookStore\Domain\ValueObject\BookDescription; |
| 80 | +use App\BookStore\Domain\ValueObject\BookName; |
| 81 | +use App\BookStore\Domain\ValueObject\Price; |
| 82 | +use App\BookStore\Infrastructure\Sylius\Resource\BookResource; |
| 83 | +use App\Shared\Application\Command\CommandBusInterface; |
| 84 | +use Sylius\Resource\Context\Context; |
| 85 | +use Sylius\Resource\Metadata\Operation; |
| 86 | +use Sylius\Resource\State\ProcessorInterface; |
| 87 | +use Webmozart\Assert\Assert; |
| 88 | + |
| 89 | +/** |
| 90 | + * @implements ProcessorInterface<BookResource> |
| 91 | + */ |
| 92 | +final readonly class CreateBookProcessor implements ProcessorInterface |
| 93 | +{ |
| 94 | + public function __construct( |
| 95 | + private CommandBusInterface $commandBus, |
| 96 | + ) { |
| 97 | + } |
| 98 | + |
| 99 | + public function process(mixed $data, Operation $operation, Context $context): BookResource |
| 100 | + { |
| 101 | + Assert::isInstanceOf($data, BookResource::class); |
| 102 | + |
| 103 | + Assert::notNull($data->name); |
| 104 | + Assert::notNull($data->description); |
| 105 | + Assert::notNull($data->author); |
| 106 | + Assert::notNull($data->content); |
| 107 | + Assert::notNull($data->price); |
| 108 | + |
| 109 | + $command = new CreateBookCommand( |
| 110 | + new BookName($data->name), |
| 111 | + new BookDescription($data->description), |
| 112 | + new Author($data->author), |
| 113 | + new BookContent($data->content), |
| 114 | + new Price($data->price), |
| 115 | + ); |
| 116 | + |
| 117 | + $model = $this->commandBus->dispatch($command); |
| 118 | + |
| 119 | + return BookResource::fromModel($model); |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
0 commit comments