refactor loading for social issues using real data

This commit is contained in:
2021-07-30 14:40:06 +02:00
committed by Marc Ducobu
parent 2f699d32b0
commit 8a962a4f1c
8 changed files with 504 additions and 349 deletions

View File

@@ -22,14 +22,20 @@
namespace Chill\PersonBundle\DataFixtures\ORM;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Chill\PersonBundle\Entity\Person;
use Faker\Factory;
use Faker\Generator;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Chill\MainBundle\DataFixtures\ORM\LoadPostalCodes;
use Chill\MainBundle\Entity\Address;
use Chill\MainBundle\Doctrine\Model\Point;
use Symfony\Component\Workflow\Registry;
use Symfony\Component\Workflow\Workflow;
use Chill\PersonBundle\Repository\SocialWork\SocialIssueRepository;
/**
* Load people into database
@@ -39,14 +45,21 @@ use Chill\MainBundle\Doctrine\Model\Point;
*/
class LoadPeople extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
protected $faker;
protected Generator $faker;
public function __construct()
protected Registry $workflowRegistry;
protected SocialIssueRepository $socialIssueRepository;
protected array $cacheSocialIssues = array();
public function __construct(Registry $workflowRegistry, SocialIssueRepository $socialIssueRepository)
{
$this->faker = \Faker\Factory::create('fr_FR');
$this->faker = Factory::create('fr_FR');
$this->workflowRegistry = $workflowRegistry;
$this->socialIssueRepository = $socialIssueRepository;
}
public function prepare()
@@ -165,7 +178,7 @@ class LoadPeople extends AbstractFixture implements OrderedFixtureInterface, Con
'Birthdate' => "1960-10-12",
'PlaceOfBirth' => "Ottignies Louvain-La-Neuve",
'Gender' => Person::MALE_GENDER,
'Email' => "Email d'un ami: roger@tt.com",
'Email' => "roger@yopmail.com",
'CountryOfBirth' => 'BE',
'Nationality' => 'BE',
'CFData' => array(),
@@ -173,6 +186,15 @@ class LoadPeople extends AbstractFixture implements OrderedFixtureInterface, Con
), $specific);
}
private function getRandomSocialIssue(): SocialIssue
{
if (0 === count($this->cacheSocialIssues)) {
$this->cacheSocialIssues = $this->socialIssueRepository->findAll();
}
return $this->cacheSocialIssues[\array_rand($this->cacheSocialIssues)];
}
/**
* create a new person from array data
*
@@ -183,14 +205,19 @@ class LoadPeople extends AbstractFixture implements OrderedFixtureInterface, Con
private function addAPerson(array $person, ObjectManager $manager)
{
$p = new Person();
$p->addAccompanyingPeriod(
new AccompanyingPeriod(
(new \DateTime())
->sub(
new \DateInterval('P'.\random_int(0, 180).'D')
)
)
$accompanyingPeriod = new AccompanyingPeriod(
(new \DateTime())
->sub(
new \DateInterval('P' . \random_int(0, 180) . 'D')
)
);
$p->addAccompanyingPeriod($accompanyingPeriod);
$accompanyingPeriod->addSocialIssue($this->getRandomSocialIssue());
if (\random_int(0, 10) > 3) {
$workflow = $this->workflowRegistry->get($accompanyingPeriod);
$workflow->apply($accompanyingPeriod, 'confirm');
}
foreach ($person as $key => $value) {
switch ($key) {