declare(strict_types=1);
namespace App\Factory;
use App\Entity\Book;
use Sylius\Resource\Factory\FactoryInterface;
final class BookFactory implements FactoryInterface
{
public function createNew(): Book
{
$book = new Book();
$book->setCreatedAt(new \DateTimeImmutable());
return $book;
}
}
declare(strict_types=1);
namespace App\Factory;
use App\Entity\Book;
use Sylius\Resource\Factory\FactoryInterface;
use Symfony\Component\Security\Core\Security;
final class BookFactory implements FactoryInterface
{
public function __construct(private Security $security)
{
}
public function createNew(): Book
{
return new Book();
}
public function createWithCreator(): Book
{
$book = $this->createNew();
$book->setCreator($this->security->getUser());
return $book;
}
}
src/Entity/Book.php
declare(strict_types=1);
namespace App\Entity\Book;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Metadata\Create;
use Sylius\Resource\Model\ResourceInterface;
#[AsResource(
operations: [
new Create(
path: 'authors/{authorId}/books',
factoryMethod: 'createWithCreator',
),
],
)
class Book implements ResourceInterface
{
}
src/Factory/BookFactory.php
declare(strict_types=1);
namespace App\Factory;
use App\Entity\Book;
use Sylius\Resource\Doctrine\Persistence\RepositoryInterface;
use Sylius\Resource\Factory\FactoryInterface;
final class BookFactory implements FactoryInterface
{
public function __construct(private RepositoryInterface $authorRepository)
{
}
public function createNew(): Book
{
return new Book();
}
public function createForAuthor(string $authorId): Book
{
$book = $this->createNew();
$author = $this->authorRepository->find($authorId);
$book->setAuthor($author);
return $book;
}
}
src/Entity/Book.php
declare(strict_types=1);
namespace App\Entity\Book;
use Sylius\Resource\Model\ResourceInterface;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Metadata\Create;
#[AsResource(
operations: [
new Create(
path: 'authors/{authorId}/books',
factoryMethod: 'createForAuthor',
factoryArguments: ['authorId' => "request.attributes.get('authorId')"],
),
],
)
class Book implements ResourceInterface
{
}
src/Entity/Book.php
declare(strict_types=1);
namespace App\Entity\Book;
use App\Factory\BookFactory;
use Sylius\Resource\Model\ResourceInterface;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Metadata\Create;
#[AsResource(
operations: [
new Create(
path: 'authors/{authorId}/books',
# Here we declared the factory to use with its fully classified class name
factory: BookFactory::class,
factoryMethod: 'createForAuthor',
factoryArguments: ['authorId' => "request.attributes.get('authorId')"],
),
],
)
class Book implements ResourceInterface
{
}
src/Factory/BookFactory.php
declare(strict_types=1);
namespace App\Factory;
use App\Entity\Book;
final class BookFactory
{
public static function create(): Book
{
return new Book();
}
}
src/Entity/Book.php
declare(strict_types=1);
namespace App\Entity\Book;
use App\Factory\BookFactory;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Metadata\Create;
use Sylius\Resource\Model\ResourceInterface
#[AsResource(
operations: [
new Create(
factory: [BookFactory::class, 'create'],
),
],
)
class Book implements ResourceInterface
{
}