periodRepository = $periodRepository; $this->evaluationRepository = $evaluationRepository; } public function getDependencies() { return [ LoadPeople::class, ]; } 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->getSocialActions()->first(); $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(); } 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)]; } }