src/JanusHercules/Shared/Presentation/Component/NavigationItemLiveComponent.php line 17

Open in your IDE?
  1. <?php
  2. namespace JanusHercules\Shared\Presentation\Component;
  3. use App\Entity\Profile\JoboffererProfile;
  4. use App\Entity\Profile\JobseekerProfile;
  5. use App\Entity\User;
  6. use Exception;
  7. use JanusHercules\Shared\Presentation\Service\HeaderPresentationService;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
  10. use Symfony\UX\LiveComponent\Attribute\LiveAction;
  11. use Symfony\UX\LiveComponent\Attribute\LiveProp;
  12. use Symfony\UX\LiveComponent\ComponentToolsTrait;
  13. use Symfony\UX\LiveComponent\DefaultActionTrait;
  14. #[AsLiveComponent(
  15. 'NavigationItem',
  16. '@janus_hercules.shared.presentation/common/navigation_item_live_component.html.twig',
  17. csrf: false,
  18. )]
  19. class NavigationItemLiveComponent extends AbstractController
  20. {
  21. use DefaultActionTrait;
  22. use ComponentToolsTrait;
  23. #[LiveProp]
  24. public ?string $icon = '';
  25. #[LiveProp]
  26. public string $url = '';
  27. #[LiveProp]
  28. public string $label = '';
  29. #[LiveProp(writable: true)]
  30. public ?string $type = '';
  31. public function __construct(
  32. private readonly HeaderPresentationService $headerPresentationService,
  33. ) {
  34. }
  35. #[LiveAction]
  36. public function needsPolling(): bool
  37. {
  38. return in_array($this->type, ['RADAR', 'MESSAGE']);
  39. }
  40. /**
  41. * @throws Exception
  42. */
  43. #[LiveAction]
  44. public function refresh(): void
  45. {
  46. $this->getNumberOfUnreadNotifications();
  47. }
  48. /**
  49. * @throws Exception
  50. */
  51. public function mount(): void
  52. {
  53. $this->getNumberOfUnreadNotifications();
  54. }
  55. /**
  56. * @throws Exception
  57. */
  58. public function getNumberOfUnreadNotifications(): int
  59. {
  60. /** @var ?User $user */
  61. $user = $this->getUser();
  62. if (is_null($user)) {
  63. return 0;
  64. }
  65. /** @var ?JobseekerProfile $jobseekerProfile */
  66. $jobseekerProfile = $user->getDefaultJobseekerProfile();
  67. if (!is_null($jobseekerProfile)) {
  68. if ($this->type === 'RADAR') {
  69. return $this->headerPresentationService->getTotalNumberOfUnreadJobradarMatches($jobseekerProfile);
  70. } elseif ($this->type === 'MESSAGE') {
  71. return $this->headerPresentationService->getUnreadMessagesForJobseekerOrJobofferer($jobseekerProfile);
  72. }
  73. }
  74. /** @var ?JoboffererProfile $joboffererProfile */
  75. $joboffererProfile = $user->getDefaultJoboffererProfile();
  76. if (!is_null($joboffererProfile)) {
  77. if ($this->type === 'MESSAGE') {
  78. return $this->headerPresentationService->getUnreadMessagesForJobseekerOrJobofferer($joboffererProfile);
  79. }
  80. }
  81. return 0;
  82. }
  83. }