em = $em; $this->process = $process; $this->storage = $storage; $this->logManager = $logManager; $this->eventDispatcher = $eventDispatcher; } /** * @param TicketMessageInterface $element * @return bool */ public function quickUpdate(TicketMessageInterface $element){ 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 string $publicKey * @return TicketMessage|null|object */ public function getByPublicKey(string $publicKey){ return $this->em->getRepository(TicketMessage::class)->findOneBy(['publicKey'=>$publicKey,'deleted'=>false]); } /** * @param TicketMessageInterface $element * @return bool */ public function checkAndValidatePublicKey(TicketMessageInterface $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 TicketMessageInterface $element * @return bool */ public function update(TicketMessageInterface $element) { return $this->quickUpdate($element); } /** * @param TicketMessage $message * @return null|array */ public function formatMessageToDisplay(TicketMessage $message){ /** * @var CustomUserInterface $user */ $user = $this->storage->getToken()->getUser(); if(!$message->isDeleted() || $user->hasRole('ROLE_EURO') || $user->hasRole('ROLE_SUPPORT_ADMIN')){ return $this->process->objectToArray($message,['id','publicKey','ostDate','content','fromUser','deleted']); } return null; } /** * @param TicketItemInterface $ticket * @param string $content * @param CustomUserInterface $creator * @return TicketMessageInterface|null */ public function create(TicketItemInterface $ticket, string $content, CustomUserInterface $creator) { $message = new TicketMessage(); $message->setDefault($ticket, $content, $creator); if(!$this->update($message)) return null; return $message; } /** * LEV : J'ai choisi de ne pas rechercher l'objet message afin d'etre sur qu'il soit dans le ticket passer en paramètre * @param TicketItem $element * @param string $message_publicKey * @return bool */ public function deleteMessage(TicketItem $element, string $message_publicKey){ $user = $this->storage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return false; foreach ($element->getMessages() as $message){ /** * @var TicketMessage $message */ if($message->getPublicKey() == $message_publicKey){ $messageId = $message->getId(); $message->setDeleted(true); if(!$this->quickUpdate($message)) return false; // Dispatch Event For Delete Message $event = new TicketLogEvent($element, TimeLogAction::DELETE_MESSAGE, $messageId); $this->eventDispatcher->dispatch(TicketLogEvent::DELETE_MESSAGE_LOG, $event); return true; } } return false; } // /** // * @param TicketItemInterface $ticketItem // * @param int $from // * @param int $to // * @param int $action // * @return bool // */ // private function addLog(TicketItemInterface $ticketItem, ?int $from = null, ?int $to = null, CustomUserInterface $user,int $action) // { // $log = $this->logManager->create($ticketItem, $from, $to, $user, $action); // // // if(is_null($log)) // return false; // // $ticketItem->addTicketLog($log); // // if(!$this->update($ticketItem)) // return false; // // return true; // } }