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.

119 lines
4.1KB

  1. <?php
  2. namespace Lc\SovBundle\Builder\Ticket;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\SovBundle\Event\Ticket\TicketEvent;
  5. use Lc\SovBundle\Notification\MailMailjetNotification;
  6. use Lc\SovBundle\Component\FormComponent;
  7. use Lc\SovBundle\Factory\Ticket\TicketFactory;
  8. use Lc\SovBundle\Model\Ticket\TicketInterface;
  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\EventDispatcher\EventDispatcher;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  15. use Symfony\Component\Security\Core\Security;
  16. class TicketBuilder
  17. {
  18. protected Security $security;
  19. protected EntityManagerInterface $entityManager;
  20. protected MailMailjetNotification $mailMailjetNotification;
  21. protected FormComponent $formComponent;
  22. protected ParameterBagInterface $parameterBag;
  23. protected TicketFactory $ticketFactory;
  24. protected AuthorizationCheckerInterface $authorizationChecker;
  25. protected UserStore $userStore;
  26. protected EventDispatcherInterface $eventDispatcher;
  27. public function __construct(
  28. Security $security,
  29. EntityManagerInterface $entityManager,
  30. MailMailjetNotification $mailMailjetNotification,
  31. FormComponent $formComponent,
  32. ParameterBagInterface $parameterBag,
  33. AuthorizationCheckerInterface $authorizationChecker,
  34. TicketFactory $ticketFactory,
  35. UserStore $userStore,
  36. EventDispatcherInterface $eventDispatcher
  37. ) {
  38. $this->security = $security;
  39. $this->entityManager = $entityManager;
  40. $this->mailMailjetNotification = $mailMailjetNotification;
  41. $this->formComponent = $formComponent;
  42. $this->parameterBag = $parameterBag;
  43. $this->ticketFactory = $ticketFactory;
  44. $this->userStore = $userStore;
  45. $this->authorizationChecker = $authorizationChecker;
  46. $this->eventDispatcher = $eventDispatcher;
  47. }
  48. public function create(array $params = []): TicketInterface
  49. {
  50. $ticket = $this->ticketFactory->create();
  51. $this->init($ticket, $params);
  52. $this->entityManager->create($ticket);
  53. $this->entityManager->flush();
  54. $this->eventDispatcher->dispatch(new TicketEvent($ticket), TicketEvent::NEW_TICKET_EVENT);
  55. return $ticket;
  56. }
  57. public function init(TicketInterface $ticket, array $params = []): void
  58. {
  59. $user = $this->security->getUser();
  60. if ($user) {
  61. $ticket->setCreatedBy($user);
  62. }
  63. if (isset($params['section'])) {
  64. $ticket->setSection($params['section']);
  65. }
  66. $ticket->setMerchant($params['merchant']);
  67. if (isset($params['user'])) {
  68. $ticket->setUser($params['user']);
  69. } else {
  70. $ticket->setVisitorFirstname($params['visitorFirstname']);
  71. $ticket->setVisitorLastname($params['visitorLastname']);
  72. $ticket->setVisitorEmail($params['visitorEmail']);
  73. $ticket->setVisitorToken(uniqid());
  74. }
  75. $ticket
  76. ->setStatus(TicketModel::TICKET_STATUS_OPEN)
  77. ->setType($params['type'])
  78. ->setSubject($params['subject']);
  79. $ticketMessageArray = $ticket->getTicketMessages();
  80. $ticketMessage = $ticketMessageArray[0];
  81. $ticketMessage->setMessage($params['message']);
  82. if (isset($params['imageFilename']) && $params['imageFilename']) {
  83. $ticketMessage->setImageFilename($params['imageFilename']);
  84. }
  85. if (isset($params['createByAdmin']) && $params['createByAdmin']) {
  86. $ticketMessage->setAnswerByAdmin(true);
  87. }
  88. }
  89. // uploadImageTicketMessage
  90. public function uploadImageTicketMessage($formTicket): ?string
  91. {
  92. return $this->formComponent->uploadFile(
  93. $formTicket,
  94. 'image',
  95. $this->parameterBag->get('app.ticket_images_directory'),
  96. $this->parameterBag->get('app.ticket_images_subdirectory')
  97. );
  98. }
  99. }