859 B
859 B
Factory
Each entity should have a factory that helps to generate a new entity.
A factory must implements App\Core\Factory\FactoryInterface
.
Implementation
A factory is basically a service which contain at lease a method named create
.
// src/Factory/MyEntityFactory;
namespace App\Factory;
use App\Core\Factory\FactoryInterface;
use App\Entity\MyEntity;
class MyEntityFactory implements FactoryInterface
{
public function create(/* specify parameters if needed */): MyEntity
{
return new MyEntity();
}
}
Usage
// src/Controller/FooController.php
namespace App\Controller;
use App\Factory\MyEntityFactory;
use Symfony\Component\HttpFoundation\Response;
class FooController
{
public function foo(MyEntityFactory $factory): Response
{
$entity = $factory->create();
// ...
}
}