mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
186 lines
5.6 KiB
PHP
186 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace Chill\PersonBundle\DataFixtures\ORM;
|
|
|
|
use Chill\PersonBundle\Entity\Person;
|
|
use Chill\PersonBundle\Entity\Household\Household;
|
|
use Chill\MainBundle\Entity\PostalCode;
|
|
use Chill\MainBundle\Entity\Address;
|
|
use Chill\PersonBundle\Household\MembersEditorFactory;
|
|
use Doctrine\Bundle\FixturesBundle\Fixture;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Doctrine\Persistence\ObjectManager;
|
|
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
|
|
use Nelmio\Alice\Loader\NativeLoader;
|
|
use Chill\MainBundle\DataFixtures\ORM\LoadPostalCodes;
|
|
|
|
class LoadHousehold extends Fixture implements DependentFixtureInterface
|
|
{
|
|
private MembersEditorFactory $editorFactory;
|
|
|
|
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)
|
|
{
|
|
// generate two times the participation. This will lead to
|
|
// some movement in participation (same people in two differents
|
|
// households)
|
|
|
|
$this->preparePersonIds();
|
|
|
|
$this->generateHousehold(
|
|
$manager,
|
|
\DateTimeImmutable::createFromFormat('Y-m-d', '2010-01-01')
|
|
);
|
|
|
|
$this->preparePersonIds();
|
|
|
|
$this->generateHousehold(
|
|
$manager,
|
|
\DateTimeImmutable::createFromFormat('Y-m-d', '2015-01-01')
|
|
);
|
|
|
|
$manager->flush();
|
|
}
|
|
|
|
private function generateHousehold(ObjectManager $manager, \DateTimeImmutable $startDate)
|
|
{
|
|
for ($i=0; $i < self::NUMBER_OF_HOUSEHOLD; $i++) {
|
|
$household = new Household();
|
|
$manager->persist($household);
|
|
|
|
$this->addAddressToHousehold($household, clone $startDate, $manager);
|
|
|
|
$movement = $this->editorFactory->createEditor($household);
|
|
|
|
// load adults
|
|
$k = 0;
|
|
foreach ($this->getRandomPersons(1, 3) as $person) {
|
|
$date = $startDate->add(new \DateInterval('P'.\random_int(1, 200).'W'));
|
|
$position = $this->getReference(LoadHouseholdPosition::ADULT);
|
|
|
|
$movement->addMovement($date, $person, $position, $k === 0, "self generated");
|
|
$k++;
|
|
}
|
|
|
|
// load children
|
|
foreach ($this->getRandomPersons(0, 3) as $person) {
|
|
$date = $startDate->add(new \DateInterval('P'.\random_int(1, 200).'W'));
|
|
$position = $this->getReference(LoadHouseholdPosition::CHILD);
|
|
|
|
$movement->addMovement($date, $person, $position, $k === 0, "self generated");
|
|
$k++;
|
|
}
|
|
|
|
// load children out
|
|
foreach ($this->getRandomPersons(0, 2) as $person) {
|
|
$date = $startDate->add(new \DateInterval('P'.\random_int(1, 200).'W'));
|
|
$position = $this->getReference(LoadHouseholdPosition::CHILD_OUT);
|
|
|
|
$movement->addMovement($date, $person, $position, $k === 0, "self generated");
|
|
$k++;
|
|
}
|
|
|
|
foreach ($movement->getPersistable() as $obj) {
|
|
$manager->persist($obj);
|
|
}
|
|
}
|
|
}
|
|
|
|
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->setValidFrom(\DateTime::createFromImmutable($date));
|
|
|
|
if (\random_int(0, 20) < 1) {
|
|
$date = $date->add(new \DateInterval('P'.\random_int(8, 52).'W'));
|
|
$address->setValidTo(\DateTime::createFromImmutable($date));
|
|
}
|
|
|
|
$household->addAddress($address);
|
|
$manager->persist($address);
|
|
|
|
$date = $date->add(new \DateInterval('P'.\random_int(8, 52).'W'));
|
|
$i++;
|
|
}
|
|
}
|
|
|
|
private function createAddress(): Address
|
|
{
|
|
$objectSet = $this->loader->loadData([
|
|
Address::class => [
|
|
'address1' => [
|
|
'street' => '<fr_FR:streetName()>',
|
|
'streetNumber' => '<fr_FR:buildingNumber()>',
|
|
'postCode' => $this->getPostalCode()
|
|
]
|
|
]
|
|
]);
|
|
|
|
return $objectSet->getObjects()['address1'];
|
|
}
|
|
|
|
private function getPostalCode(): PostalCode
|
|
{
|
|
$ref = LoadPostalCodes::$refs[\array_rand(LoadPostalCodes::$refs)];
|
|
|
|
return $this->getReference($ref);
|
|
}
|
|
|
|
private function preparePersonIds()
|
|
{
|
|
$this->personIds = $this->em
|
|
->createQuery('SELECT p.id FROM '.Person::class.' p '.
|
|
'JOIN p.center c '.
|
|
'WHERE c.name = :center '
|
|
)
|
|
->setParameter('center', 'Center A')
|
|
->getScalarResult()
|
|
;
|
|
|
|
\shuffle($this->personIds);
|
|
}
|
|
|
|
private function getRandomPersons(int $min, int $max)
|
|
{
|
|
$nb = \random_int($min, $max);
|
|
|
|
for ($i=0; $i < $nb; $i++) {
|
|
$personId = \array_pop($this->personIds)['id'];
|
|
$persons[] = $this->em->getRepository(Person::class)
|
|
->find($personId)
|
|
;
|
|
}
|
|
|
|
return $persons ?? [];
|
|
}
|
|
|
|
public function getDependencies()
|
|
{
|
|
return [
|
|
LoadPeople::class,
|
|
LoadHouseholdPosition::class
|
|
];
|
|
}
|
|
}
|