first impl of person Mover + add fixtures

This commit is contained in:
2021-05-28 16:41:37 +02:00
parent 94bcbac06a
commit 87ba68971c
13 changed files with 384 additions and 21 deletions

View File

@@ -0,0 +1,101 @@
<?php
namespace Chill\PersonBundle\DataFixtures\ORM;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Household\MoveMembersFactory;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
class LoadHousehold extends Fixture implements DependentFixtureInterface
{
private MoveMembersFactory $movementFactory;
private EntityManagerInterface $em;
private CONST NUMBER_OF_HOUSEHOLD = 10;
public function __construct(MoveMembersFactory $movementFactory, EntityManagerInterface $em)
{
$this->movementFactory = $movementFactory;
$this->em = $em;
}
public function load(ObjectManager $manager)
{
$this->preparePersonIds();
for ($i=0; $i < self::NUMBER_OF_HOUSEHOLD; $i++) {
$household = new Household();
$manager->persist($household);
$movement = $this->movementFactory->createMovement($household);
// load adults
$k = 0;
foreach ($this->getRandomPersons(1, 3) as $person) {
$date = \DateTimeImmutable::createFromFormat('Y-m-d', '2010-01-01')
->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 = \DateTimeImmutable::createFromFormat('Y-m-d', '2010-01-01')
->add(new \DateInterval('P'.\random_int(1, 200).'W'));
$position = $this->getReference(LoadHouseholdPosition::CHILD);
$movement->addMovement($date, $person, $position, $k === 0, "self generated");
$k++;
}
foreach ($movement->getPersistable() as $obj) {
print($obj->getStartDate()->format('Y-m-d'));
$manager->persist($obj);
}
}
$manager->flush();
}
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
];
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace Chill\PersonBundle\DataFixtures\ORM;
use Chill\PersonBundle\Entity\Household\Position;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
class LoadHouseholdPosition extends Fixture
{
const POSITIONS_DATA = [
["Adulte", true, true, 1.0, self::ADULT ],
["Enfants", true, false, 2.0, self::CHILD ],
["Enfants hors ménage", false, false, 3.0, self::CHILD_OUT ]
];
const ADULT = "position_adulte";
const CHILD = "position_enfant";
const CHILD_OUT = "position_enfant_hors";
public function load(ObjectManager $manager)
{
foreach (self::POSITIONS_DATA as list($name, $share, $allowHolder,
$ordering, $ref)) {
$position = (new Position())
->setLabel([ "fr" => $name ])
->setAllowHolder($allowHolder)
->setShareHousehold($share)
->setOrdering($ordering)
;
$manager->persist($position);
$this->addReference($ref, $position);
}
$manager->flush();
}
}