em = $em; $this->rs = $rs; $this->storage = $storage; $this->locale = $rs->getCurrentRequest()->getLocale(); $this->groupManagerService = $groupManagerService; $this->class = $class; } /** * @param array $languages * @return FormForgeReason|null */ public function create(array $languages){ $formForgeReason = new FormForgeReason(); $nameLinguistica = new LinguisticaTradItem(); $nameLinguistica->setStrValues($languages); $formForgeReason->setDefault($nameLinguistica); if(!$this->quickUpdate($formForgeReason)) return null; return $formForgeReason; } /** * @param string $publicKey * @return FormForgeReason|null|object */ public function getByPublicKey(string $publicKey){ return $this->em->getRepository($this->class)->findOneBy(['publicKey'=>$publicKey,'deleted'=>false]); } /** * @return FormForgeReasonInterface[]|array|object[] */ public function getAll(){ return $this->em->getRepository($this->class)->findBy(['deleted'=>false]); } /** * @param FormForgeReasonInterface $element * @return bool */ public function quickUpdate(FormForgeReasonInterface $element): bool{ try { if(!$this->em->contains($element)){ $r = $this->checkAndValidatePublicKey($element); if($r) $this->em->persist($element); else return false; } $this->em->flush(); } catch (OptimisticLockException $e) { //die('Exception : '.$e->getMessage()); //throw new Exception('Exception : '.$e->getMessage()); return false; } catch (ORMException $e) { return false; } return true; } /** * @param FormForgeReasonInterface $element * @return bool */ public function checkAndValidatePublicKey(FormForgeReasonInterface $element):bool{ $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 FormForgeReasonInterface $element * @return bool */ public function update(FormForgeReasonInterface $element){ if(empty($element)) return false; return $this->quickUpdate($element); } /** * @param FormForgeReasonInterface $element * @param CustomUserInterface|null $user * @return bool */ public function canEdit(FormForgeReasonInterface $element, ?CustomUserInterface $user = null){ if(is_null($user)) { $user = $this->storage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return false; } if($element->isDeleted()) return false; if($user->hasRole('ROLE_SUPPORT_ADMIN')) return true; return false; } /** * @param FormForgeReasonInterface $element * @param CustomUserInterface|null $user * @return bool */ public function canView(FormForgeReasonInterface $element, ?CustomUserInterface $user = null){ if(is_null($user)) { $user = $this->storage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return false; } if($element->isDeleted()) return false; if($user->hasRole('ROLE_SUPPORT') || $user->hasRole('ROLE_SUPPORT_ADMIN')) return true; return false; } /** * @param CustomUserInterface|null $user * @return bool */ public function canList(?CustomUserInterface $user = null){ if(is_null($user)) { $user = $this->storage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return false; } if($user->hasRole('ROLE_SUPPORT') || $user->hasRole('ROLE_SUPPORT_ADMIN')) return true; return false; } /** * @param FormForgeReasonInterface $element * @param CustomUserInterface|null $user * @return bool */ public function canDelete(FormForgeReasonInterface $element, ?CustomUserInterface $user = null){ if(is_null($user)) { $user = $this->storage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return false; } if($element->isDeleted()) return false; //TODO hasRole on User if($user->hasRole('ROLE_SUPPORT_ADMIN')) return true; return false; } /** * @param FormForgeReasonInterface $element * @param CompanyService $compagnyService * @return bool */ public function addService(FormForgeReasonInterface $element, CompanyService $compagnyService){ $element->setCompanyService($compagnyService); return $this->quickUpdate($element); } /** * @param FormForgeReasonInterface $element * @param CompanyService $compagnyService * @return bool */ public function updateService(FormForgeReasonInterface $element, CompanyService $compagnyService){ $element->setCompanyService($compagnyService); return $this->quickUpdate($element); } /** * @param FormForgeReasonInterface $element * @param CompanyService $compagnyService * @return bool */ public function removeService(FormForgeReasonInterface $element, CompanyService $compagnyService){ $element->setCompanyService($compagnyService); return $this->quickUpdate($element); } /** * @param FormForgeReasonInterface $element * @param UserGroup $group * @return bool */ public function addUserGroup(FormForgeReasonInterface $element, UserGroup $group){ $element->setSelfGroup($group); return $this->quickUpdate($element); } public function updateSelfGroup(FormForgeReasonInterface $element, UserGroup $group){ $element->setSelfGroup($group); return $this->quickUpdate($element); } /** * @param FormForgeReasonInterface $element * @param UserGroup $group * @return bool */ public function removeUserGroup(FormForgeReasonInterface $element, UserGroup $group){ $element->setSelfGroup($group); return $this->quickUpdate($element); } /** * @param string $publicKey * @return bool */ public function delete(string $publicKey){ $element = $this->getByPublicKey($publicKey); $element->setDeleted(true); return $this->quickUpdate($element); } /** * @param FormForgeReasonInterface $element * @return bool */ private function deleteOnError(FormForgeReasonInterface $element) { try { $this->em->remove($element); $this->em->flush(); } catch (ORMException $e) { return false; } return true; } /** * @param FormForgeReasonInterface $element * @param FormForgeFieldInterface $formForgeFieldElement * @return bool */ public function addFormForgeField(FormForgeReasonInterface $element, FormForgeFieldInterface $formForgeFieldElement){ $element->addFormForgeField($formForgeFieldElement); return $this->quickUpdate($element); } /** * @param string $publicKey * @param array $languages * @return FormForgeReason|bool|null|object */ public function updateReason(string $publicKey, array $languages){ $formForgeReason = $this->getByPublicKey($publicKey); $formForgeReason->getName()->setStrValues($languages); if(empty($formForgeReason)) return false; $this->quickUpdate($formForgeReason); return $formForgeReason; } public function getOneById($id) { return $this->em->getRepository($this->class)->findOneBy(['id'=>$id,'deleted'=>false]); } }