chill-bundles/src/Bundle/ChillPersonBundle/DataFixtures/ORM/LoadAccompanyingPeriodWork.php

141 lines
4.8 KiB
PHP

<?php
namespace Chill\PersonBundle\DataFixtures\ORM;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\SocialWork\Evaluation;
use Chill\PersonBundle\Entity\SocialWork\SocialAction;
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
use Chill\PersonBundle\Repository\AccompanyingPeriodRepository;
use Chill\PersonBundle\Repository\SocialWork\EvaluationRepository;
use Chill\PersonBundle\Repository\SocialWork\SocialActionRepository;
use Chill\PersonBundle\Repository\SocialWork\SocialIssueRepository;
use Doctrine\Persistence\ObjectManager;
use Chill\PersonBundle\DataFixtures\ORM\LoadPeople;
class LoadAccompanyingPeriodWork extends \Doctrine\Bundle\FixturesBundle\Fixture implements \Doctrine\Common\DataFixtures\DependentFixtureInterface
{
private AccompanyingPeriodRepository $periodRepository;
private EvaluationRepository $evaluationRepository;
/**
* @var Evaluation[]|array
*/
private array $cacheEvaluations = [];
/**
* @param AccompanyingPeriodRepository $periodRepository
*/
public function __construct(
AccompanyingPeriodRepository $periodRepository,
EvaluationRepository $evaluationRepository
) {
$this->periodRepository = $periodRepository;
$this->evaluationRepository = $evaluationRepository;
}
/**
* @inheritDoc
*/
public function getDependencies()
{
return [
LoadPeople::class
];
}
private function getRandomAction(SocialIssue $socialIssue): ?SocialAction
{
$actions = $socialIssue->getRecursiveSocialActions()->toArray();
if (0 === count($actions)) {
return null;
}
return $actions[\array_rand($actions)];
}
private function getRandomEvaluation(): Evaluation
{
if (0 === count($this->cacheEvaluations)) {
$this->cacheEvaluations = $this->evaluationRepository
->findAll();
}
return $this->cacheEvaluations[\array_rand($this->cacheEvaluations)];
}
/**
* @inheritDoc
*/
public function load(ObjectManager $manager)
{
// load all the period which are confirmed
$periods = $this->periodRepository
->findBy(['step' => AccompanyingPeriod::STEP_CONFIRMED])
;
$i = 0;
while (null !== $period = \array_pop($periods)) {
/** @var AccompanyingPeriod $period */
$i++;
if (0 === $i % 15) {
$manager->flush();
}
if (0 === $period->getSocialIssues()->count()) {
continue;
}
$work = new AccompanyingPeriod\AccompanyingPeriodWork();
$action = $this->getRandomAction($period->getSocialIssues()->first());
if (null !== $action) {
$work
->setAccompanyingPeriod($period)
->setSocialAction($action)
->setStartDate(new \DateTimeImmutable('today'))
->addPerson($period->getPersons()->first())
->setCreatedAt(new \DateTimeImmutable())
->setCreatedBy($this->getReference('center a_social'))
->setUpdatedAt(new \DateTimeImmutable())
->setUpdatedBy($this->getReference('center a_social'));
$manager->persist($work);
if ($action->getEvaluations()->count() > 0) {
$ev = $action->getEvaluations()->first();
$evaluation = new AccompanyingPeriod\AccompanyingPeriodWorkEvaluation();
$evaluation->setAccompanyingPeriodWork($work)
->setEvaluation($ev);
$manager->persist($evaluation);
}
}
// 1 of 10, force an evaluation
if (0 === $i % 10) {
$evaluation = $this->getRandomEvaluation();
$action = $evaluation->getSocialAction();
$issue = $action->getIssue();
$period->addSocialIssue($issue);
$work
->setAccompanyingPeriod($period)
->setSocialAction($action)
->setStartDate(new \DateTimeImmutable('today'))
->addPerson($period->getPersons()->first())
->setCreatedAt(new \DateTimeImmutable())
->setCreatedBy($this->getReference('center a_social'))
->setUpdatedAt(new \DateTimeImmutable())
->setUpdatedBy($this->getReference('center a_social'))
;
$manager->persist($work);
$ev = new AccompanyingPeriod\AccompanyingPeriodWorkEvaluation();
$ev->setAccompanyingPeriodWork($work)
->setEvaluation($evaluation);
$manager->persist($ev);
}
}
$manager->flush();
}
}