TicketUtils.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\Model\Ticket;
  10. class TicketUtils
  11. {
  12. protected $em ;
  13. protected $merchantUtils ;
  14. protected $mailUtils ;
  15. public function __construct(EntityManagerInterface $em, MerchantUtilsInterface $merchantUtils, MailUtils $mailUtils)
  16. {
  17. $this->em = $em ;
  18. $this->merchantUtils = $merchantUtils ;
  19. $this->mailUtils = $mailUtils ;
  20. }
  21. public function createTicket($params): TicketInterface
  22. {
  23. $classTicket = $this->em->getClassMetadata(TicketInterface::class)->getName() ;
  24. $ticket = new $classTicket ;
  25. $ticket->setMerchant($this->merchantUtils->getMerchantCurrent()) ;
  26. $ticket->setStatus(1) ;
  27. if(isset($params['user'])) {
  28. $ticket->setUser($params['user']) ;
  29. $email = $params['user']->getEmail() ;
  30. $firstname = $params['user']->getFirstname() ;
  31. }
  32. else {
  33. $ticket->setVisitorFirstname($params['visitorFirstname']) ;
  34. $ticket->setVisitorLastname($params['visitorLastname']) ;
  35. $ticket->setVisitorEmail($params['visitorEmail']) ;
  36. $ticket->setVisitorToken(uniqid()) ;
  37. $email = $params['visitorEmail'] ;
  38. $firstname = $params['visitorFirstname'] ;
  39. }
  40. $ticket->setStatus(Ticket::TICKET_STATUS_OPEN) ;
  41. $ticket->setType($params['type']) ;
  42. if(isset($params['orderShop']) && $params['orderShop'] && $params['orderShop'] instanceof OrderShopInterface) {
  43. $ticket->setOrderShop($params['orderShop']) ;
  44. }
  45. $ticket->setSubject($params['subject']) ;
  46. $this->em->persist($ticket);
  47. $classTicketMessage = $this->em->getClassMetadata(TicketMessageInterface::class)->getName() ;
  48. $ticketMessage = new $classTicketMessage ;
  49. $ticketMessage->setStatus(1) ;
  50. $ticketMessage->setTicket($ticket) ;
  51. $ticketMessage->setMessage($params['message']) ;
  52. $this->em->persist($ticketMessage);
  53. $this->em->flush() ;
  54. // envoi email au client
  55. $this->mailUtils->send([
  56. MailUtils::SUBJECT => 'Nouvelle demande',
  57. MailUtils::TO_EMAIL => $email,
  58. MailUtils::CONTENT_TEMPLATE => 'mail/ticket-new',
  59. MailUtils::CONTENT_DATA => [
  60. 'firstname' => $firstname,
  61. 'ticket' => $ticket
  62. ],
  63. ]) ;
  64. return $ticket ;
  65. }
  66. public function createTicketMessage($params)
  67. {
  68. $classTicketMessage = $this->em->getClassMetadata(TicketMessageInterface::class)->getName() ;
  69. $ticketMessage = new $classTicketMessage ;
  70. $ticket = $params['ticket'] ;
  71. $ticketMessage->setStatus(1) ;
  72. $ticketMessage->setTicket($ticket) ;
  73. $ticketMessage->setMessage($params['message']) ;
  74. if(isset($params['answerByAdmin']) && $params['answerByAdmin']) {
  75. $ticketMessage->setAnswerByAdmin($params['answerByAdmin']) ;
  76. // envoi email au client
  77. $this->mailUtils->send([
  78. MailUtils::SUBJECT => 'Réponse à votre demande',
  79. MailUtils::TO_EMAIL => $ticket->getUser() ? $ticket->getUser()->getEmail() : $ticket->getVisitorEmail(),
  80. MailUtils::CONTENT_TEMPLATE => 'mail/ticket-response',
  81. MailUtils::CONTENT_DATA => [
  82. 'firstname' => $ticket->getUser() ? $ticket->getUser()->getFirstname() : $ticket->getVisitorFirstname(),
  83. 'ticket' => $ticket
  84. ],
  85. ]) ;
  86. }
  87. $this->em->persist($ticketMessage);
  88. if(isset($params['closeTicket']) && $params['closeTicket']) {
  89. $ticket->setStatus(Ticket::TICKET_STATUS_CLOSED) ;
  90. }
  91. $ticket->setUpdatedAt(new \DateTime()) ;
  92. $this->em->persist($ticket);
  93. $this->em->flush() ;
  94. return $ticketMessage ;
  95. }
  96. }