cs: Fix code style (safe rules only).

This commit is contained in:
Pol Dellaiera
2021-11-23 14:06:38 +01:00
parent 149d7ce991
commit 8f96a1121d
1223 changed files with 65199 additions and 64625 deletions

View File

@@ -1,31 +1,45 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\DataFixtures\ORM;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\MainBundle\Entity\PostalCode;
use Chill\MainBundle\DataFixtures\ORM\LoadPostalCodes;
use Chill\MainBundle\Entity\Address;
use Chill\MainBundle\Entity\PostalCode;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Household\MembersEditorFactory;
use DateInterval;
use DateTime;
use DateTimeImmutable;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Nelmio\Alice\Loader\NativeLoader;
use Chill\MainBundle\DataFixtures\ORM\LoadPostalCodes;
use function array_pop;
use function array_rand;
use function random_int;
use function shuffle;
class LoadHousehold extends Fixture implements DependentFixtureInterface
{
private const NUMBER_OF_HOUSEHOLD = 10;
private MembersEditorFactory $editorFactory;
private EntityManagerInterface $em;
private NativeLoader $loader;
private CONST NUMBER_OF_HOUSEHOLD = 10;
private array $personIds;
public function __construct(MembersEditorFactory $editorFactory, EntityManagerInterface $em)
@@ -35,6 +49,14 @@ class LoadHousehold extends Fixture implements DependentFixtureInterface
$this->loader = new NativeLoader();
}
public function getDependencies()
{
return [
LoadPeople::class,
LoadHouseholdPosition::class,
];
}
public function load(ObjectManager $manager)
{
// generate two times the participation. This will lead to
@@ -45,87 +67,44 @@ class LoadHousehold extends Fixture implements DependentFixtureInterface
$this->generateHousehold(
$manager,
\DateTimeImmutable::createFromFormat('Y-m-d', '2010-01-01')
DateTimeImmutable::createFromFormat('Y-m-d', '2010-01-01')
);
$this->preparePersonIds();
$this->generateHousehold(
$manager,
\DateTimeImmutable::createFromFormat('Y-m-d', '2015-01-01')
DateTimeImmutable::createFromFormat('Y-m-d', '2015-01-01')
);
$manager->flush();
}
private function generateHousehold(ObjectManager $manager, \DateTimeImmutable $startDate)
private function addAddressToHousehold(Household $household, DateTimeImmutable $date, ObjectManager $manager)
{
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) {
if (random_int(0, 10) > 8) {
// 20% of household without address
return;
}
$nb = \random_int(1, 6);
$nb = random_int(1, 6);
$i = 0;
while ($i < $nb) {
$address = $this->createAddress();
$address->setValidFrom(\DateTime::createFromImmutable($date));
$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));
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++;
$date = $date->add(new DateInterval('P' . random_int(8, 52) . 'W'));
++$i;
}
}
@@ -136,54 +115,92 @@ class LoadHousehold extends Fixture implements DependentFixtureInterface
'address1' => [
'street' => '<fr_FR:streetName()>',
'streetNumber' => '<fr_FR:buildingNumber()>',
'postCode' => $this->getPostalCode()
]
]
'postCode' => $this->getPostalCode(),
],
],
]);
return $objectSet->getObjects()['address1'];
}
private function getPostalCode(): PostalCode
private function generateHousehold(ObjectManager $manager, DateTimeImmutable $startDate)
{
$ref = LoadPostalCodes::$refs[\array_rand(LoadPostalCodes::$refs)];
for ($i = 0; self::NUMBER_OF_HOUSEHOLD > $i; ++$i) {
$household = new Household();
$manager->persist($household);
return $this->getReference($ref);
$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, 0 === $k, '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, 0 === $k, '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, 0 === $k, 'self generated');
++$k;
}
foreach ($movement->getPersistable() as $obj) {
$manager->persist($obj);
}
}
}
private function preparePersonIds()
private function getPostalCode(): PostalCode
{
// @TODO: Remove this and make this service stateless
$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();
$ref = LoadPostalCodes::$refs[array_rand(LoadPostalCodes::$refs)];
\shuffle($this->personIds);
return $this->getReference($ref);
}
private function getRandomPersons(int $min, int $max): array
{
$persons = [];
$nb = \random_int($min, $max);
$nb = random_int($min, $max);
for ($i=0; $i < $nb; $i++) {
$personId = \array_pop($this->personIds)['id'];
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()
private function preparePersonIds()
{
return [
LoadPeople::class,
LoadHouseholdPosition::class
];
// @TODO: Remove this and make this service stateless
$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);
}
}