/** * Create (register) or update driving assertion plans * * @param RepeatedDrivingAssertionRegisterDTO $assertionDTO * @param Driver $driver * @return array * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ protected function registerOrUpdateAssertionPlan(RepeatedDrivingAssertionRegisterDTO $assertionDTO, Driver $driver) { /** @var RepeatedDrivingAssertionPlanRepositoryDoctrine $assertionPlanRepository */ $assertionPlanRepository = $this->get('repeateddrivingassertionplan_repository'); /** @var RepeatedDrivingAssertionRepositoryDoctrine $assertionRepository */ $assertionRepository = $this->get('repeateddrivingassertion_repository'); /** @var RepeatedDrivingAssertionAssembler $assembler*/ $assembler = $this->get('tixi_api.repeateddrivingassertionplanassembler'); /** @var DrivingAssertionManagement $damService */ $damService = $this->get('tixi_app.drivingassertionmanagement'); /** @var RepeatedDrivingAssertionPlan $assertionPlan*/ $assertionPlan = null; if (null === $assertionDTO->id) { //create new $assertionPlan = $assembler->repeatedRegisterDTOToNewDrivingAssertionPlan($assertionDTO); $driver->assignRepeatedDrivingAssertionPlan($assertionPlan); } else { //update $assertionPlan = $assertionPlanRepository->find($assertionDTO->id); $assembler->repeatedRegisterDTOToDrivingAssertionPlan($assertionPlan, $assertionDTO); foreach ($assertionPlan->getRepeatedDrivingAssertions() as $previousAssertions) { $assertionRepository->remove($previousAssertions); } } $repeatedAssertions = new ArrayCollection(); if ($assertionDTO->frequency === 'weekly') { $repeatedAssertions = $assembler->repeatedRegisterDTOtoWeeklyDrivingAssertions($assertionDTO); } else { if ($assertionDTO->frequency === 'monthly') { $repeatedAssertions = $assembler->repeatedRegisterDTOtoMonthlyDrivingAssertions($assertionDTO); } else { throw $this->createNotFoundException('Wrong frequency(' . $assertionDTO->frequency . ') found, expected "weekly" or "monthly".'); } } // check the driving assertion plan and it's repeated driving assertions $validationErrors = $damService->checkAssertionPlan($assertionPlan, $repeatedAssertions); if (count($validationErrors) != 0) { return $validationErrors; } // handle new and changed driving assertions /** @var RepeatedDrivingAssertion $repeatedAssertion */ foreach ($repeatedAssertions as $repeatedAssertion) { $repeatedAssertion->setAssertionPlan($assertionPlan); $assertionRepository->store($repeatedAssertion); } $assertionPlan->replaceRepeatedDrivingAssertions($repeatedAssertions); $assertionPlanRepository->store($assertionPlan); if (null === $assertionDTO->id) { $validationErrors = $damService->handleNewRepeatedDrivingAssertion($assertionPlan); } else { $validationErrors = $damService->handleChangeInRepeatedDrivingAssertion($assertionPlan); } return $validationErrors; }