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.

161 lines
6.2KB

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