entityManager = $_container->get('doctrine.orm.entity_manager'); $this->class = $_class; $this->locale = $_container->get('request_stack')->getCurrentRequest()->getLocale(); $this->repository = $this->entityManager->getRepository($_class); $this->eventDispatcher = $_container->get('event_dispatcher'); } // public function getClass(){ // if (false !== strpos($this->class_reason_response, ':')){ // $metadata = $this->entityManager->getClassMetadata($this->class_reason_response); // $this->class_reason_response = $metadata->getName(); // } // return $this->class_reason_response; // } /** * @param FormForgeReasonResponseInterface $element * @return bool */ public function quickUpdate(FormForgeReasonResponseInterface $element){ if(empty($element)) return false; try { if(!$this->entityManager->contains($element)){ $r = $this->checkAndValidatePublicKey($element); if($r) $this->entityManager->persist($element); else return false; } $this->entityManager->flush(); } catch (OptimisticLockException $e) { return false; } catch (ORMException $e) { return false; } return true; } /** * @param FormForgeReasonResponseInterface $element * @return bool */ public function checkAndValidatePublicKey(FormForgeReasonResponseInterface $element){ $attempts = 0; while(true){ $u = $this->getByPublicKey($element->getPublicKey()); if(is_null($u)) return true; $element->regeneratePublicKey(); $attempts++; if($attempts > 20) return false; } return false; } /** * @param FormForgeReasonResponseInterface $element * @return bool */ public function update(FormForgeReasonResponseInterface $element){ if(empty($element)) return false; return $this->quickUpdate($element); } /** * @return array|object[] */ public function getAll() { return $this->repository->findAll(); } /** * @param $publicKey * @return null|object */ public function getByPublicKey($publicKey){ return $this->repository->findOneBy(['publicKey' => $publicKey]); } /** * @param FormForgeReasonInterface $reason * @param string $locale * @param CustomUserInterface $user * @return FormForgeReasonInterface|null * @throws \Exception */ public function createReasonResponse(FormForgeReasonInterface $reason, string $locale, CustomUserInterface $user = null): ?FormForgeReasonResponseInterface { // if(is_null($user)) { // $user = $this->storage->getToken()->getUser(); // // if(!$user instanceof CustomUserInterface) // return null; // } /** * @var $reasonResponse FormForgeReasonResponseInterface */ //$class = $this->getClass($this->class_reason_response); // $reasonResponse = new $this->class_reason_response; $reasonResponse = new FormForgeReasonResponse(); // Hydrate object $reasonResponse->regeneratePublicKey(); if ($user != null){ $reasonResponse->setUser($user); } $reasonResponse->setDateResponse(new \DateTime('now')); $reasonResponse->setReason($reason); $reasonResponse->setLocale($locale); // Register object $register = $this->update($reasonResponse); if (!$register) return null; return $reasonResponse; } public function callEvents(FormForgeReasonResponse $reasonResponse){ // Dispatch event for Create TicketItem foreach ($reasonResponse->getReason()->getFormForgeMappingEvents() as $eventName){ $event = new FormForgeReasonResponseEvent($reasonResponse); $this->eventDispatcher->dispatch($eventName->getName(), $event); } } /** * @param FormForgeReasonResponseInterface $reasonResponse * @param FormForgeFieldResponseInterface $fieldResponse * @return bool */ public function addFieldsValues(FormForgeReasonResponseInterface $reasonResponse, FormForgeFieldResponseInterface $fieldResponse){ $reasonResponse->addFieldResponse($fieldResponse); return $this->update($reasonResponse); } }