mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 13:54:23 +00:00
[WIP] add fixtures for household address
This commit is contained in:
parent
35d35c9f40
commit
8bd20c9c78
@ -9,6 +9,7 @@ use Doctrine\Bundle\FixturesBundle\Fixture;
|
|||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Doctrine\Persistence\ObjectManager;
|
use Doctrine\Persistence\ObjectManager;
|
||||||
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
|
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
|
||||||
|
use Nelmio\Alice\Loader\NativeLoader;
|
||||||
|
|
||||||
class LoadHousehold extends Fixture implements DependentFixtureInterface
|
class LoadHousehold extends Fixture implements DependentFixtureInterface
|
||||||
{
|
{
|
||||||
@ -16,12 +17,15 @@ class LoadHousehold extends Fixture implements DependentFixtureInterface
|
|||||||
|
|
||||||
private EntityManagerInterface $em;
|
private EntityManagerInterface $em;
|
||||||
|
|
||||||
|
private NativeLoader $loader;
|
||||||
|
|
||||||
private CONST NUMBER_OF_HOUSEHOLD = 10;
|
private CONST NUMBER_OF_HOUSEHOLD = 10;
|
||||||
|
|
||||||
public function __construct(MembersEditorFactory $editorFactory, EntityManagerInterface $em)
|
public function __construct(MembersEditorFactory $editorFactory, EntityManagerInterface $em)
|
||||||
{
|
{
|
||||||
$this->editorFactory = $editorFactory;
|
$this->editorFactory = $editorFactory;
|
||||||
$this->em = $em;
|
$this->em = $em;
|
||||||
|
$this->loader = new NativeLoader();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function load(ObjectManager $manager)
|
public function load(ObjectManager $manager)
|
||||||
@ -53,6 +57,8 @@ class LoadHousehold extends Fixture implements DependentFixtureInterface
|
|||||||
$household = new Household();
|
$household = new Household();
|
||||||
$manager->persist($household);
|
$manager->persist($household);
|
||||||
|
|
||||||
|
$this->addAddressToHousehold($household, \DateTimeImmutable::createFrom($startDate), $manager);
|
||||||
|
|
||||||
$movement = $this->editorFactory->createEditor($household);
|
$movement = $this->editorFactory->createEditor($household);
|
||||||
|
|
||||||
// load adults
|
// 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()
|
private function preparePersonIds()
|
||||||
{
|
{
|
||||||
$this->personIds = $this->em
|
$this->personIds = $this->em
|
||||||
|
@ -68,13 +68,14 @@ class Household
|
|||||||
*/
|
*/
|
||||||
public function addAddress(Address $address)
|
public function addAddress(Address $address)
|
||||||
{
|
{
|
||||||
$this->addresses[] = $address;
|
|
||||||
|
|
||||||
foreach ($this->getAddresses() as $a) {
|
foreach ($this->getAddresses() as $a) {
|
||||||
if ($a->getValidFrom() < $address->getValidFrom() && $a->getValidTo() === NULL) {
|
if ($a->getValidFrom() < $address->getValidFrom() && $a->getValidTo() === NULL) {
|
||||||
$a->setValidTo($address->getValidFrom());
|
$a->setValidTo($address->getValidFrom());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->addresses[] = $address;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,17 +88,8 @@ class LoadThirdParty extends Fixture Implements DependentFixtureInterface
|
|||||||
private function getPostalCode(): PostalCode
|
private function getPostalCode(): PostalCode
|
||||||
{
|
{
|
||||||
$ref = LoadPostalCodes::$refs[\array_rand(LoadPostalCodes::$refs)];
|
$ref = LoadPostalCodes::$refs[\array_rand(LoadPostalCodes::$refs)];
|
||||||
|
|
||||||
return $this->getReference($ref);
|
return $this->getReference($ref);
|
||||||
if (count($this->postalCodesIds) === 0) {
|
|
||||||
// fill the postal codes
|
|
||||||
$this->em->createQuery('SELECT p.id FROM '.PostalCode::class)
|
|
||||||
->getScalarResult();
|
|
||||||
}
|
|
||||||
|
|
||||||
$id = $this->postalCodesIds[\array_rand($this->postalCodesIds)];
|
|
||||||
|
|
||||||
return $this->em->getRepository(PostalCode::class)
|
|
||||||
->find($id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createAddress(): ObjectSet
|
private function createAddress(): ObjectSet
|
||||||
|
Loading…
x
Reference in New Issue
Block a user