You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
5.9KB

  1. <?php
  2. namespace Lc\SovBundle\Builder\Ticket;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\CaracoleBundle\Notification\MailMailjetNotification;
  5. use Lc\SovBundle\Component\FormComponent;
  6. use Lc\SovBundle\Factory\Ticket\TicketFactory;
  7. use Lc\SovBundle\Model\Ticket\TicketInterface;
  8. use Lc\SovBundle\Model\Ticket\TicketMessageInterface;
  9. use Lc\SovBundle\Model\Ticket\TicketModel;
  10. use Lc\SovBundle\Repository\User\UserStore;
  11. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  12. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  13. class TicketBuilder
  14. {
  15. protected EntityManagerInterface $entityManager;
  16. protected MailMailjetNotification $mailMailjetNotification;
  17. protected FormComponent $formComponent;
  18. protected ParameterBagInterface $parameterBag;
  19. protected TicketFactory $ticketFactory;
  20. protected AuthorizationCheckerInterface $authorizationChecker;
  21. protected UserStore $userStore;
  22. public function __construct(
  23. EntityManagerInterface $entityManager,
  24. MailMailjetNotification $mailMailjetNotification,
  25. FormComponent $formComponent,
  26. ParameterBagInterface $parameterBag,
  27. AuthorizationCheckerInterface $authorizationChecker,
  28. TicketFactory $ticketFactory,
  29. UserStore $userStore
  30. ) {
  31. $this->entityManager = $entityManager;
  32. $this->mailMailjetNotification = $mailMailjetNotification;
  33. $this->formComponent = $formComponent;
  34. $this->parameterBag = $parameterBag;
  35. $this->ticketFactory = $ticketFactory;
  36. $this->userStore = $userStore;
  37. }
  38. public function create(array $params = []): TicketInterface
  39. {
  40. $ticket = $this->ticketFactory->create();
  41. $this->init($ticket, $params);
  42. $user = $ticket->getUser();
  43. if ($ticket->getUser()) {
  44. $email = $user->getEmail();
  45. $firstname = $user->getFirstname();
  46. } else {
  47. $email = $params['visitorEmail'];
  48. $firstname = $params['visitorFirstname'];
  49. }
  50. if (isset($params['createByAdmin']) && $params['createByAdmin']) {
  51. // envoi email au client
  52. $this->mailMailjetNotification->send(
  53. [
  54. MailMailjetNotification::SUBJECT => 'Vous avez reçu un nouveau message',
  55. MailMailjetNotification::TO_EMAIL => $email,
  56. MailMailjetNotification::CONTENT_TEMPLATE => 'mail/ticket-new-by-admin',
  57. MailMailjetNotification::CONTENT_DATA => [
  58. 'firstname' => $firstname,
  59. 'ticket' => $ticket,
  60. ],
  61. ]
  62. );
  63. } else {
  64. $this->mailMailjetNotification->send(
  65. [
  66. MailMailjetNotification::SUBJECT => 'Nouvelle demande',
  67. MailMailjetNotification::TO_EMAIL => $email,
  68. MailMailjetNotification::CONTENT_TEMPLATE => 'mail/ticket-new',
  69. MailMailjetNotification::CONTENT_DATA => [
  70. 'firstname' => $firstname,
  71. 'ticket' => $ticket,
  72. ],
  73. ]
  74. );
  75. }
  76. $this->entityManager->persist($ticket);
  77. $this->entityManager->flush();
  78. // notifyAdmin
  79. $usersToNotify = $this->userStore->getByTicketTypesNotification($ticket->getType());
  80. foreach ($usersToNotify as $userToNotify) {
  81. if ($this->authorizationChecker->isGranted('ROLE_ADMIN', $userToNotify)) {
  82. $this->mailMailjetNotification->send(
  83. [
  84. MailMailjetNotification::SUBJECT => 'Nouveau ticket sur Place du Local',
  85. MailMailjetNotification::TO_EMAIL => $userToNotify->getEmail(),
  86. MailMailjetNotification::CONTENT_TEMPLATE => 'mail/ticket-notification',
  87. MailMailjetNotification::CONTENT_DATA => [
  88. 'firstname' => $userToNotify->getFirstname(),
  89. 'ticket' => $ticket,
  90. 'ticketMessage' => $ticket->getTicketMessage(),
  91. ],
  92. ]
  93. );
  94. }
  95. }
  96. return $ticket;
  97. }
  98. public function init(TicketInterface $ticket, array $params = []): void
  99. {
  100. if (isset($params['user'])) {
  101. $ticket->setUser($params['user']);
  102. } else {
  103. $ticket->setVisitorFirstname($params['visitorFirstname']);
  104. $ticket->setVisitorLastname($params['visitorLastname']);
  105. $ticket->setVisitorEmail($params['visitorEmail']);
  106. $ticket->setVisitorToken(uniqid());
  107. }
  108. $ticket
  109. ->setStatus(TicketModel::TICKET_STATUS_OPEN)
  110. ->setType($params['type'])
  111. ->setSubject($params['subject']);
  112. $ticketMessage = $ticket->getTicketMessage();
  113. $ticketMessage->setStatus(1);
  114. $ticketMessage->setMessage($params['message']);
  115. if (isset($params['imageFilename']) && $params['imageFilename']) {
  116. $ticketMessage->setImageFilename($params['imageFilename']);
  117. }
  118. if (isset($params['createByAdmin']) && $params['createByAdmin']) {
  119. $ticketMessage->setAnswerByAdmin(true);
  120. }
  121. }
  122. // uploadImageTicketMessage
  123. public function uploadImageTicketMessage($formTicket): ?string
  124. {
  125. return $this->formComponent->uploadFile(
  126. $formTicket,
  127. 'image',
  128. $this->parameterBag->get('app.ticket_images_directory'),
  129. $this->parameterBag->get('app.ticket_images_subdirectory')
  130. );
  131. }
  132. }