mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 22:53:49 +00:00
[WIP] add fixtures for household address
This commit is contained in:
@@ -9,6 +9,7 @@ use Doctrine\Bundle\FixturesBundle\Fixture;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\Persistence\ObjectManager;
|
||||
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
|
||||
use Nelmio\Alice\Loader\NativeLoader;
|
||||
|
||||
class LoadHousehold extends Fixture implements DependentFixtureInterface
|
||||
{
|
||||
@@ -16,12 +17,15 @@ class LoadHousehold extends Fixture implements DependentFixtureInterface
|
||||
|
||||
private EntityManagerInterface $em;
|
||||
|
||||
private NativeLoader $loader;
|
||||
|
||||
private CONST NUMBER_OF_HOUSEHOLD = 10;
|
||||
|
||||
public function __construct(MembersEditorFactory $editorFactory, EntityManagerInterface $em)
|
||||
{
|
||||
$this->editorFactory = $editorFactory;
|
||||
$this->em = $em;
|
||||
$this->loader = new NativeLoader();
|
||||
}
|
||||
|
||||
public function load(ObjectManager $manager)
|
||||
@@ -53,6 +57,8 @@ class LoadHousehold extends Fixture implements DependentFixtureInterface
|
||||
$household = new Household();
|
||||
$manager->persist($household);
|
||||
|
||||
$this->addAddressToHousehold($household, \DateTimeImmutable::createFrom($startDate), $manager);
|
||||
|
||||
$movement = $this->editorFactory->createEditor($household);
|
||||
|
||||
// load adults
|
||||
@@ -89,6 +95,55 @@ class LoadHousehold extends Fixture implements DependentFixtureInterface
|
||||
}
|
||||
}
|
||||
|
||||
private function addAddressToHousehold(Household $household, \DateTimeImmutable $date, ObjectManager $manager)
|
||||
{
|
||||
if (\random_int(0, 10) > 8) {
|
||||
// 20% of household without address
|
||||
return;
|
||||
}
|
||||
|
||||
$nb = \random_int(1, 6);
|
||||
|
||||
$i = 0;
|
||||
while ($i < $nb) {
|
||||
$address = $this->createAddress();
|
||||
$address->validFrom($date);
|
||||
|
||||
if (\random_int(0, 20) < 1) {
|
||||
$date = $date->add(new \DateInterval('P'.\random_int(8, 52).'W'));
|
||||
$address->validTo($date);
|
||||
}
|
||||
|
||||
$household->addAddress($address);
|
||||
$manager->persist($address);
|
||||
|
||||
$date = $date->add(new \DateInterval('P'.\random_int(8, 52).'W'));
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
|
||||
private function createAddress()
|
||||
{
|
||||
$objectSet = $this->loader->loadData([
|
||||
Address::class => [
|
||||
'address1' => [
|
||||
'street' => '<fr_FR:streetName()>',
|
||||
'streetNumber' => '<fr_FR:buildingNumber()>',
|
||||
'postCode' => $this->getPostalCode()
|
||||
]
|
||||
]
|
||||
]);
|
||||
|
||||
return $objectSet->getObjects()[0];
|
||||
}
|
||||
|
||||
private function getPostalCode(): PostalCode
|
||||
{
|
||||
$ref = LoadPostalCodes::$refs[\array_rand(LoadPostalCodes::$refs)];
|
||||
|
||||
return $this->getReference($ref);
|
||||
}
|
||||
|
||||
private function preparePersonIds()
|
||||
{
|
||||
$this->personIds = $this->em
|
||||
|
Reference in New Issue
Block a user