mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +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\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
|
||||
|
@ -68,13 +68,14 @@ class Household
|
||||
*/
|
||||
public function addAddress(Address $address)
|
||||
{
|
||||
$this->addresses[] = $address;
|
||||
|
||||
foreach ($this->getAddresses() as $a) {
|
||||
if ($a->getValidFrom() < $address->getValidFrom() && $a->getValidTo() === NULL) {
|
||||
$a->setValidTo($address->getValidFrom());
|
||||
}
|
||||
}
|
||||
|
||||
$this->addresses[] = $address;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
@ -88,17 +88,8 @@ class LoadThirdParty extends Fixture Implements DependentFixtureInterface
|
||||
private function getPostalCode(): PostalCode
|
||||
{
|
||||
$ref = LoadPostalCodes::$refs[\array_rand(LoadPostalCodes::$refs)];
|
||||
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user