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 line
3.1KB

  1. <?php
  2. namespace domain\Ticket\Ticket;
  3. use domain\Ticket\TicketMessage\TicketMessage;
  4. use domain\Ticket\TicketUser\TicketUser;
  5. use domain\User\User\User;
  6. use domain\User\User\UserSolver;
  7. use domain\_\AbstractSolver;
  8. class TicketSolver extends AbstractSolver
  9. {
  10. protected UserSolver $userSolver;
  11. public function loadDependencies(): void
  12. {
  13. $this->userSolver = $this->loadService(UserSolver::class);
  14. }
  15. public function hasTicketAccess(Ticket $ticket, User $user): string
  16. {
  17. return $user->id_producer == $ticket->id_producer
  18. || $this->userSolver->isAdmin($user);
  19. }
  20. public function isTicketOpen(Ticket $ticket)
  21. {
  22. return $ticket->status == Ticket::STATUS_OPEN;
  23. }
  24. public function isTicketClosed(Ticket $ticket)
  25. {
  26. return $ticket->status == Ticket::STATUS_CLOSED;
  27. }
  28. public function getTicketDateCreatedAtFormat(Ticket $ticket)
  29. {
  30. return date('d/m/Y', strtotime($ticket->created_at));
  31. }
  32. public function getTicketStatusLabelAsHtml(Ticket $ticket)
  33. {
  34. $classLabel = 'label-success';
  35. $statusLabel = 'Ouvert';
  36. if ($this->isTicketClosed($ticket)) {
  37. $classLabel = 'label-danger';
  38. $statusLabel = 'Fermé';
  39. }
  40. return '<span class="label ' . $classLabel . '">' . $statusLabel . '</span>';
  41. }
  42. public function getTicketUser(Ticket $ticket, User $user)
  43. {
  44. foreach($ticket->ticketUsers as $ticketUser) {
  45. if($ticketUser->id_user == $user->id) {
  46. return $ticketUser;
  47. }
  48. }
  49. return null;
  50. }
  51. public function isTicketMessageUnread(TicketMessage $ticketMessage, TicketUser $ticketUser)
  52. {
  53. if($ticketMessage->created_at > $ticketUser->read_at) {
  54. return true;
  55. }
  56. return false;
  57. }
  58. public function isTicketUnread(Ticket $ticket, User $user): bool
  59. {
  60. $ticketUser = $this->getTicketUser($ticket, $user);
  61. if($ticketUser) {
  62. foreach($ticket->ticketMessages as $ticketMessage) {
  63. if($this->isTicketMessageUnread($ticketMessage, $ticketUser)) {
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69. return true;
  70. }
  71. public function getFirstTicketMessageUnread(Ticket $ticket, User $user): ?TicketMessage
  72. {
  73. $ticketUser = $this->getTicketUser($ticket, $user);
  74. if($ticketUser) {
  75. foreach($ticket->ticketMessages as $ticketMessage) {
  76. if($this->isTicketMessageUnread($ticketMessage, $ticketUser)) {
  77. return $ticketMessage;
  78. }
  79. }
  80. }
  81. if(isset($ticket->ticketMessages[0])) {
  82. return $ticket->ticketMessages[0];
  83. }
  84. return null;
  85. }
  86. public function countTicketsUnreadByUserFromArray(array $ticketsArray, User $user): int
  87. {
  88. $count = 0;
  89. foreach($ticketsArray as $ticket) {
  90. if($this->isTicketUnread($ticket, $user)) {
  91. $count++;
  92. }
  93. }
  94. return $count;
  95. }
  96. }