em = $em; $this->process = $process; $this->storage = $storage; $this->logManager = $logManager; $this->bundleManager = $bundleManager; $this->messageManager = $messageManager; $this->eventDispatcher = $eventDispatcher; $this->companyServiceManager= $companyServiceManager; $this->userManager = $userManager; } // /** // * @param string $class // * @return string // */ // public function getClass(string $class){ // if (false !== strpos($class, ':')) { // $metadata = $this->em->getClassMetadata($class); // $class = $metadata->getName(); // } // // return $class; // } /** * @param string $publicKey * @return TicketItem|null|object */ public function getByPublicKey(string $publicKey){ return $this->em->getRepository(TicketItem::class)->findOneBy(['publicKey'=>$publicKey,'deleted'=>false]); } /** * @return TicketItem[]|array */ public function getAll(){ return $this->em->getRepository(TicketItem::class)->findBy(['deleted'=>false]); } /** * @param FormForgeReasonResponseInterface $reasonResponse * @return TicketItemInterface|null */ public function create(FormForgeReasonResponseInterface $reasonResponse){ $ticket = new TicketItem(); $ticket->setDefault($reasonResponse); if(!$this->quickUpdate($ticket)) return null; if(!$this->bundleManager->createTicketFolder($ticket)){ $this->deleteOnError($ticket); return null; } if(!$this->quickUpdate($ticket)) return null; $ticket->setStatus(TicketItemStatus::WAIT_INTERVENANT); if(!$this->update($ticket)) return null; // Dispatch Event for Create log $event = new TicketLogEvent($ticket, TimeLogAction::STATUS_TICKET); $this->eventDispatcher->dispatch(TicketLogEvent::CREAT_LOG, $event); return $ticket; } /** * @param string $_subject * @param string $_message * @param string $_locale * @param UserBase $_interveningUser * @return TicketItem|null */ public function createManually(string $_subject, string $_message, string $_locale, UserBase $_interveningUser){ $ticket = new TicketItem(); $ticket->setSubject($_subject); $ticket->setLocale($_locale); $ticket->setInterveningUser($_interveningUser); if(!$this->update($ticket)) return null; $message = $this->messageManager->create($ticket, $_message, $_interveningUser); $ticket->addMessage($message); if(!$this->update($ticket)) return null; return $ticket; } /** * @param TicketItemInterface $ticket * @param string $userPublicKey * @param TicketMessageInterface $comment * @return bool */ public function updateIntervening(TicketItemInterface $ticket, string $userPublicKey, ?TicketMessageInterface $comment=null){ $intervening = $this->userManager->getUserByPublicKey($userPublicKey); if (is_null($ticket->getInterveningUser())){ $current_user = $this->storage->getToken()->getUser(); }else{ $current_user = $ticket->getInterveningUser(); } if(!$current_user instanceof CustomUserInterface) return false; if(is_null($intervening)){ $ticket->setInterveningUser($current_user); } else { $ticket->setInterveningUser($intervening); $ticket->addMessage($comment); } $ticket->setStatus(TicketItemStatus::IN_PROGRESS); if(!$this->update($ticket)) return false; // Dispatch Event for Create log $event = new TicketLogEvent($ticket, TimeLogAction::CHANGE_INTERVENING); $this->eventDispatcher->dispatch(TicketLogEvent::CHANGE_INTERVENING_LOG, $event); return true; } /** * @param TicketItemInterface $element * @param string $message * @param CustomUserInterface|null $creator * @return TicketItemInterface|bool|null */ public function addMessage(TicketItemInterface $element, string $message, ?CustomUserInterface $creator = null){ $user = $this->storage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return null; if(is_null($creator)) { $creator = $this->storage->getToken()->getUser(); if(!$creator instanceof CustomUserInterface) return null; } // Creation of message $message = $this->messageManager->create($element,$message,$creator); if(is_null($message)){ // The message could not be created return false; } else $element->addMessage($message); if(!$this->update($element)) return false; // Dispatch Event For Add Message $event = new TicketLogEvent($element, TimeLogAction::ADD_MESSAGE); $this->eventDispatcher->dispatch(TicketLogEvent::ADD_MESSAGE_LOG, $event); return $element; } /** * * @param TicketItemInterface $element * @return TicketItemInterface|bool|null */ public function removeIntervening(TicketItemInterface $element){ $user = $this->storage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return null; $element->setInterveningUser(null); if(!$this->update($element)) return false; // Dispatch Event For Remove Message $event = new TicketLogEvent($element, TimeLogAction::DELETE_INTERVENING); $this->eventDispatcher->dispatch(TicketLogEvent::REMOVE_INTERVENING_LOG, $event); return $element; } /** * @param TicketItemInterface $element * @param string $comment * @param bool $closed * @return TicketItemInterface|bool * @throws \Exception */ public function close(TicketItemInterface $element, string $comment, bool $closed){ $user = $this->storage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return false; if(!$this->addMessage($element,$comment,$user)) return false; $now = new \DateTime(); $element->setCloseDate($now); $element->setClosed($closed); $element->setStatus(TicketItemStatus::CLOSED); if(!$this->update($element)) return false; // Dispatch Event For Close Ticket $event = new TicketLogEvent($element, TimeLogAction::STATUS_TICKET_CLOSED); $this->eventDispatcher->dispatch(TicketLogEvent::CLOSE_TICKET_LOG, $event); return $element; } /** * @param TicketItemInterface $element * @param string $comment * @param bool $resolve * @return TicketItemInterface|bool * @throws \Exception */ public function resolve(TicketItemInterface $element, string $comment, bool $resolve){ $user = $this->storage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return false; if(!$this->addMessage($element,$comment,$user)) return false; $now = new \DateTime(); $element->setResolvedDate($now); $element->setResolve($resolve); $element->setStatus(TicketItemStatus::RESOLVED); if(!$this->update($element)) return false; // Dispatch Event For Resolve status $event = new TicketLogEvent($element, TimeLogAction::STATUS_TICKET_RESOLVED); $this->eventDispatcher->dispatch(TicketLogEvent::RESOLVE_TICKET_LOG, $event); return $element; } public function updateCompanyService(TicketItemInterface $ticket, string $companyServicePublicKey, ?TicketMessageInterface $comment=null){ // Recuperate company service $companyService = $this->companyServiceManager->getByPublicKey($companyServicePublicKey); if (is_null($companyService)) return null; // Recuperate user $user = $this->storage->getToken()->getUser(); if (!$user instanceof CustomUserInterface) return false; // Create Message if not null if (!is_null($comment)){ $ticket = $this->addMessage($ticket, $comment, $user); } $ticket->setCompanyServiceIntervene($companyService); $ticket->setStatus(TicketItemStatus::IN_PROGRESS); if (!$this->quickUpdate($ticket)) return false; // Dispatch Event For update company service $event = new TicketLogEvent($ticket, TimeLogAction::CHANGE_SERVICE); $this->eventDispatcher->dispatch(TicketLogEvent::CHANGE_SERVICE_LOG, $event); return $ticket; } public function deleteCompanyService(string $publicKye){ $ticket = $this->getByPublicKey($publicKye); if (!$ticket instanceof TicketItemInterface) return null; $ticket->setCompanyServiceIntervene(null); if (!$this->quickUpdate($ticket)) return false; // Dispatch event for removing company service $event = new TicketLogEvent($ticket, TimeLogAction::DELETE_SERVICE); $this->eventDispatcher->dispatch(TicketLogEvent::REMOVE_SERVICE_LOG); return $ticket; } /** * @param TicketItem $element * @param CustomUserInterface|null $user * @return bool */ public function canUse(TicketItem $element, ?CustomUserInterface $user = null){ if(is_null($user)) { $user = $this->storage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return false; } if($element->isDeleted()) return false; //que es admin du support car sinon l'utilisateur doit être dans les intervenants if($user->hasRole('ROLE_SUPPORT_ADMIN')) return true; if($element->getCustomer() == $user) return true; $intervening = $element->getInterveningUser(); if(!is_null($intervening)) return true; return false; } /** * @param TicketItem $element * @param CustomUserInterface|null $user * @return bool */ public function canEdit(TicketItem $element, ?CustomUserInterface $user = null){ if(is_null($user)) { $user = $this->storage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return false; } if($element->isDeleted()) return false; //que es admin du support car sinon l'utilisateur doit être dans les intervenants if($user->hasRole('ROLE_SUPPORT_ADMIN')) return true; if($element->getCustomer() == $user) return true; $intervening = $element->getInterveningUser(); if(!is_null($intervening)) return true; return false; } /** * @param TicketItem $element * @param CustomUserInterface|null $user * @return bool */ public function canView(TicketItem $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; if($element->getCustomer() == $user) 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->getId() == 1){ $user->addRole('ROLE_SUPPORT_ADMIN'); try { $this->em->flush(); } catch (OptimisticLockException $e) { } catch (ORMException $e) { } }*/ if($user->hasRole('ROLE_SUPPORT') || $user->hasRole('ROLE_SUPPORT_ADMIN')) return true; return false; } /** * @param TicketItem $element * @param CustomUserInterface|null $user * @return bool */ public function canDelete(TicketItem $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; if($element->getCustomer() == $user) return true; return false; } /** * @param TicketItemInterface $element * @return bool */ public function quickUpdate(TicketItemInterface $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 TicketItem $element * @return bool */ public function checkAndValidatePublicKey(TicketItem $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 TicketItemInterface $element * @return bool */ public function update(TicketItemInterface $element) { return $this->quickUpdate($element); } /** * @param TicketItem $element * @return bool */ public function delete(TicketItem $element){ $element->setDeleted(true); return $this->quickUpdate($element); } /** * Utilisé uniquement en interne pour supprimer un ticket dont la box n'a pas pu être créée * @param TicketItemInterface $ticket */ private function deleteOnError(TicketItemInterface $ticket){ try { $this->em->remove($ticket); $this->em->flush(); } catch (ORMException $e) { } } /** * @param TicketItemInterface $ticketItem * @param int $from * @param int|null $to * @param CustomUserInterface $user * @param int $action * @return bool */ public function addLog(TicketItemInterface $ticketItem, int $from, 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; } }