em = $em; $this->repository = $this->em->getRepository(FormForgeMappingEvent::class); $this->formForgeReasonManager = $_forgeReasonManager; } /** * @param FormForgeMappingEvent $element * @return bool */ private function quickUpdate(FormForgeMappingEvent $element){ if(empty($element)) return false; try { $this->em->persist($element); $this->em->flush(); } catch (ORMException $e) { return false; } return true; } private function delete($_id){ $element = $this->getById($_id); if($this->em->contains($element)){ try { $this->em->remove($element); $this->em->flush(); } catch (ORMException $e) { return false; } } return true; } /** * @param int $_id * @return FormForgeMappingEvent|object|null */ public function getById(int $_id){ return $this->repository->findOneBy(['id'=>$_id]); } /** * @return array|FormForgeMappingEvent[]|object[] */ public function getAll(){ return $this->repository->findAll(); } /** * @param string $_name * @param FormForgeReason $formForgeReason * @return FormForgeMappingEvent|null */ public function createEventName(string $_name, FormForgeReason $formForgeReason){ $element = new FormForgeMappingEvent(); $element->setName($_name); $element->setFormForgeReason($formForgeReason); if (!$this->quickUpdate($element)) return null; $formForgeReason->addFormForgeMappingEvent($element); $this->formForgeReasonManager->quickUpdate($formForgeReason); return $element; } /** * @param int $_id * @return bool|null */ public function deleteEventName(int $_id){ if (!$this->delete($_id)) return null; return true; } public function addEventToReason(FormForgeReason $reason, array $data){ // Delete the first line named _csrf_token form array $data unset($data['_csrf_token']); // Recuperate event and add it inside reason foreach ($data as $id){ $event = $this->getById($id); $reason->addFormForgeMappingEvent($event); $this->formForgeReasonManager->quickUpdate($reason); } return $reason; } }