選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

151 行
6.4KB

  1. <?php
  2. namespace Lc\ShopBundle\Services ;
  3. use App\Entity\TicketMessage;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  6. use Lc\ShopBundle\Context\OrderShopInterface;
  7. use Lc\ShopBundle\Context\TicketInterface;
  8. use Lc\ShopBundle\Context\TicketMessageInterface;
  9. use Lc\ShopBundle\Context\UserInterface;
  10. use Lc\ShopBundle\Model\Ticket;
  11. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  12. class TicketUtils
  13. {
  14. protected $em ;
  15. protected $merchantUtils ;
  16. protected $mailUtils ;
  17. protected $authorizationChecker ;
  18. public function __construct(EntityManagerInterface $em, MerchantUtilsInterface $merchantUtils, MailUtils $mailUtils, AuthorizationCheckerInterface $authorizationChecker)
  19. {
  20. $this->em = $em ;
  21. $this->merchantUtils = $merchantUtils ;
  22. $this->mailUtils = $mailUtils ;
  23. $this->authorizationChecker = $authorizationChecker ;
  24. }
  25. public function createTicket($params): TicketInterface
  26. {
  27. $classTicket = $this->em->getClassMetadata(TicketInterface::class)->getName() ;
  28. $ticket = new $classTicket ;
  29. $ticket->setMerchant($this->merchantUtils->getMerchantCurrent()) ;
  30. $ticket->setStatus(1) ;
  31. if(isset($params['user'])) {
  32. $ticket->setUser($params['user']) ;
  33. $email = $params['user']->getEmail() ;
  34. $firstname = $params['user']->getFirstname() ;
  35. }
  36. else {
  37. $ticket->setVisitorFirstname($params['visitorFirstname']) ;
  38. $ticket->setVisitorLastname($params['visitorLastname']) ;
  39. $ticket->setVisitorEmail($params['visitorEmail']) ;
  40. $ticket->setVisitorToken(uniqid()) ;
  41. $email = $params['visitorEmail'] ;
  42. $firstname = $params['visitorFirstname'] ;
  43. }
  44. $ticket->setStatus(Ticket::TICKET_STATUS_OPEN) ;
  45. $ticket->setType($params['type']) ;
  46. if(isset($params['orderShop']) && $params['orderShop'] && $params['orderShop'] instanceof OrderShopInterface) {
  47. $ticket->setOrderShop($params['orderShop']) ;
  48. }
  49. $ticket->setSubject($params['subject']) ;
  50. $this->em->persist($ticket);
  51. $classTicketMessage = $this->em->getClassMetadata(TicketMessageInterface::class)->getName() ;
  52. $ticketMessage = new $classTicketMessage ;
  53. $ticketMessage->setStatus(1) ;
  54. $ticketMessage->setTicket($ticket) ;
  55. $ticketMessage->setMessage($params['message']) ;
  56. $this->em->persist($ticketMessage);
  57. $this->em->flush() ;
  58. // envoi email au client
  59. $this->mailUtils->send([
  60. MailUtils::SUBJECT => 'Nouvelle demande',
  61. MailUtils::TO_EMAIL => $email,
  62. MailUtils::CONTENT_TEMPLATE => 'mail/ticket-new',
  63. MailUtils::CONTENT_DATA => [
  64. 'firstname' => $firstname,
  65. 'ticket' => $ticket
  66. ],
  67. ]) ;
  68. $this->notifyAdmin($ticket, $ticketMessage);
  69. return $ticket ;
  70. }
  71. public function createTicketMessage($params)
  72. {
  73. $classTicketMessage = $this->em->getClassMetadata(TicketMessageInterface::class)->getName() ;
  74. $ticketMessage = new $classTicketMessage ;
  75. $ticket = $params['ticket'] ;
  76. $ticketMessage->setStatus(1) ;
  77. $ticketMessage->setTicket($ticket) ;
  78. $ticketMessage->setMessage($params['message']) ;
  79. if(isset($params['answerByAdmin']) && $params['answerByAdmin']) {
  80. $ticketMessage->setAnswerByAdmin($params['answerByAdmin']) ;
  81. // envoi email au client
  82. $this->mailUtils->send([
  83. MailUtils::SUBJECT => 'Réponse à votre demande',
  84. MailUtils::TO_EMAIL => $ticket->getUser() ? $ticket->getUser()->getEmail() : $ticket->getVisitorEmail(),
  85. MailUtils::CONTENT_TEMPLATE => 'mail/ticket-response',
  86. MailUtils::CONTENT_DATA => [
  87. 'firstname' => $ticket->getUser() ? $ticket->getUser()->getFirstname() : $ticket->getVisitorFirstname(),
  88. 'ticket' => $ticket
  89. ],
  90. ]) ;
  91. }
  92. $this->em->persist($ticketMessage);
  93. if(isset($params['closeTicket']) && $params['closeTicket']) {
  94. $ticket->setStatus(Ticket::TICKET_STATUS_CLOSED) ;
  95. }
  96. $ticket->setUpdatedAt(new \DateTime()) ;
  97. $this->em->persist($ticket);
  98. $this->em->flush() ;
  99. return $ticketMessage ;
  100. }
  101. public function notifyAdmin($ticket, $ticketMessage){
  102. $userRepo = $this->em->getRepository(UserInterface::class);
  103. $usersToNotify = $userRepo->findByTicketTypesNotification($ticket->getType());
  104. foreach ($usersToNotify as $userToNotify){
  105. if($this->authorizationChecker->isGranted('ROLE_ADMIN', $userToNotify)){
  106. // envoi email au client
  107. $this->mailUtils->send([
  108. MailUtils::SUBJECT => 'Nouveau ticket sur placedulocal',
  109. MailUtils::TO_EMAIL => $userToNotify->getEmail(),
  110. MailUtils::CONTENT_TEMPLATE => 'mail/ticket-notification',
  111. MailUtils::CONTENT_DATA => [
  112. 'firstname' => $userToNotify->getFirstname(),
  113. 'ticket' => $ticket,
  114. 'ticketMessage' => $ticketMessage
  115. ],
  116. ]) ;
  117. }
  118. }
  119. }
  120. }