src/Controller/ExpertProfileController.php line 133

Open in your IDE?
  1. <?php
  2. namespace MedBrief\MSR\Controller;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use MedBrief\MSR\Entity\Expert;
  5. use MedBrief\MSR\Entity\Project;
  6. use MedBrief\MSR\Entity\User;
  7. use MedBrief\MSR\Repository\ExpertRepository;
  8. use MedBrief\MSR\Repository\ExpertUserRepository;
  9. use MedBrief\MSR\Repository\MatchLetterRepository;
  10. use MedBrief\MSR\Service\File\FileHandler\VichUploaderFileHandler;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. /**
  17. * @Route("/user/profile")
  18. */
  19. class ExpertProfileController extends BaseController
  20. {
  21. /**
  22. * @Route(
  23. * "/my-profile/{reactRouting}",
  24. * name="app_expert_profile",
  25. * requirements={"reactRouting"=".*"},
  26. * defaults={"reactRouting"=""},
  27. * methods={"GET"}
  28. * )
  29. *
  30. * @Security("is_granted('VIEW_MY_PROFILE')")
  31. *
  32. * @param Request $request
  33. * @param ExpertRepository $expertRepository
  34. * @param ExpertUserRepository $expertUserRepository
  35. * @param EntityManagerInterface $entityManager
  36. */
  37. public function indexAction(Request $request, EntityManagerInterface $entityManager, ExpertUserRepository $expertUserRepository): Response
  38. {
  39. /** @var User $user */
  40. $user = $this->getUser();
  41. // This allows any user that is linked to an expert entity to access the profile, not just the primary user.
  42. $expertUser = $expertUserRepository->getExpertByUser($user);
  43. // Create an expert profile if the user does not have one, and they are allowed to have one.
  44. if (!($expertUser instanceof Expert)) {
  45. $expertUser = new Expert();
  46. $expertUser->setUser($user);
  47. $expertUser->setCreatedBy($user);
  48. // Since the user is creating their own profile, it will automatically be in the expert reviewed status.
  49. $expertUser->setReviewedBy(Expert::REVIEWED_BY_EXPERT_REVIEWED);
  50. $entityManager->persist($expertUser);
  51. $entityManager->flush();
  52. }
  53. return $this->render('UserProfile/profile.html.twig', [
  54. 'currentExpertUserId' => $expertUser->getId(),
  55. 'user' => $expertUser,
  56. 'activeTab' => 'my-profile',
  57. ]);
  58. }
  59. /**
  60. * @Route(
  61. * "/verify-my-profile/{reactRouting}",
  62. * name="app_expert_verify_profile",
  63. * requirements={"reactRouting"=".*"},
  64. * defaults={"reactRouting"=""},
  65. * methods={"GET"}
  66. * )
  67. *
  68. * @Security("is_granted('VIEW_MY_PROFILE')")
  69. *
  70. * @param Request $request
  71. * @param ExpertUserRepository $expertUserRepository
  72. * @param MatchLetterRepository $matchLetterRepository
  73. */
  74. public function verifyProfileAction(Request $request, ExpertUserRepository $expertUserRepository): Response
  75. {
  76. /** @var User $user */
  77. $user = $this->getUser();
  78. // This allows any user that is linked to an expert entity to access the profile, not just the primary user.
  79. $expertUser = $expertUserRepository->getExpertByUser($user);
  80. // At verify stage, we do not create an Expert profile as they should not feature on Match if they don't have an Expert profile.
  81. if (!($expertUser instanceof Expert)) {
  82. throw $this->createAccessDeniedException('User has no expert profile associated with their account.');
  83. }
  84. // Add a flash message if one is passed through.
  85. $responseMessage = $request->query->get('message', null);
  86. if ($responseMessage) {
  87. $this->addFlash('success', $responseMessage);
  88. }
  89. if ($expertUser->isExpertReviewed() === true) {
  90. return $this->redirectToRoute('infology_medbrief_dashboard');
  91. }
  92. $reviewedByExpert = $expertUser->isExpertReviewed();
  93. return $this->render('UserProfile/verifyProfile.html.twig', [
  94. 'currentExpertUserId' => $expertUser->getId(),
  95. 'user' => $expertUser,
  96. 'reviewedByExpert' => $reviewedByExpert,
  97. ]);
  98. }
  99. /**
  100. * This end-point streams an Expert's profile photo.
  101. *
  102. * @Route(
  103. * "/photo/expert/{id}/{projectId}",
  104. * name="app_expert_profile_photo",
  105. * methods={"GET"},
  106. * defaults={"projectId"=null}
  107. * )
  108. *
  109. * @ParamConverter("paramUser", class="MedBrief\MSR\Entity\User", options={"id" = "id"})
  110. * @ParamConverter("paramProject", class="MedBrief\MSR\Entity\Project", options={"id" = "projectId"}, isOptional=true)
  111. *
  112. * @param Request $request
  113. * @param User $paramUser
  114. * @param Project|null $paramProject
  115. * @param VichUploaderFileHandler $fileHandler
  116. * @param ExpertUserRepository $expertUserRepository
  117. *
  118. * @return Response
  119. */
  120. public function streamProfilePhotoAction(
  121. Request $request,
  122. User $paramUser,
  123. ?Project $paramProject = null,
  124. ExpertUserRepository $expertUserRepository,
  125. VichUploaderFileHandler $fileHandler
  126. ) {
  127. $viewMyProfile = $this->isGranted('VIEW_MY_PROFILE');
  128. $viewFirm = $paramProject ? $this->isGranted('MATCH_VIEW_FIRM', $paramProject) : false;
  129. if (!$viewMyProfile && !$viewFirm) {
  130. throw $this->createAccessDeniedException();
  131. }
  132. // This allows any user that is linked to an expert entity to access the profile, not just the primary user.
  133. $expertUser = $expertUserRepository->getExpertByUser($paramUser);
  134. if (!($expertUser instanceof Expert)) {
  135. throw $this->createNotFoundException('No expert profile associated with this user.');
  136. }
  137. // Grab the expert's profile photo filename to see if it exists.
  138. if (!$expertUser->getProfilePhotoFilename()) {
  139. throw $this->createNotFoundException();
  140. }
  141. $response = $fileHandler->serveAzureBlob($request, $expertUser, 'profilePhotoFile');
  142. return $response;
  143. }
  144. }