src/Controller/Page/JobAlertController.php line 154

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Page;
  3. use App\Component\Configuration\Util\Config;
  4. use App\Entity\JobAlert;
  5. use App\Entity\JobAlertSetting;
  6. use App\Entity\OptionValue;
  7. use App\Event\JobAlertCreatedEvent;
  8. use App\EventListener\FeatureFlagListener;
  9. use App\Form\CompanyJobAlertType;
  10. use App\Form\JobAlertType;
  11. use App\Geocode\Coordinates;
  12. use App\Geocode\Geocode;
  13. use App\Renderer\Page;
  14. use App\Service\CompanyService;
  15. use App\Service\JobAlertService;
  16. use App\Service\LocalisedSettingService;
  17. use App\Service\MotionRecruitService;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. use Flagception\Manager\FeatureManagerInterface;
  20. use GuzzleHttp\Exception\GuzzleException;
  21. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  22. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  23. use Symfony\Component\HttpFoundation\JsonResponse;
  24. use Symfony\Component\HttpFoundation\RedirectResponse;
  25. use Symfony\Component\HttpFoundation\Request;
  26. use Symfony\Component\HttpFoundation\Response;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  29. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  30. use Symfony\Component\Serializer\Serializer;
  31. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  32. use Symfony\Contracts\Translation\TranslatorInterface;
  33. use Twig\Error\LoaderError as Twig_Error_Loader;
  34. use Twig\Error\RuntimeError as Twig_Error_Runtime;
  35. use Twig\Error\SyntaxError as Twig_Error_Syntax;
  36. #[Route(path'/jobalert')]
  37. class JobAlertController extends AbstractController
  38. {
  39.     private Page $pageRenderer;
  40.     private Geocode $geocode;
  41.     private JobAlertService $jobAlertService;
  42.     private FeatureManagerInterface $featureManager;
  43.     private TranslatorInterface $translator;
  44.     private LocalisedSettingService $localisedSettingService;
  45.     private EventDispatcherInterface $eventDispatcher;
  46.     private CompanyService $companyService;
  47.     private Config $config;
  48.     public function __construct(
  49.         Page $pageRenderer,
  50.         Geocode $geocode,
  51.         JobAlertService $jobAlertService,
  52.         FeatureManagerInterface $featureManager,
  53.         TranslatorInterface $translator,
  54.         LocalisedSettingService $localisedSettingService,
  55.         EventDispatcherInterface $eventDispatcher,
  56.         CompanyService $companyService,
  57.         Config $config,
  58.         private readonly EntityManagerInterface $entityManager,
  59.     ) {
  60.         $this->pageRenderer $pageRenderer;
  61.         $this->geocode $geocode;
  62.         $this->jobAlertService $jobAlertService;
  63.         $this->featureManager $featureManager;
  64.         $this->translator $translator;
  65.         $this->localisedSettingService $localisedSettingService;
  66.         $this->eventDispatcher $eventDispatcher;
  67.         $this->companyService $companyService;
  68.         $this->config $config;
  69.     }
  70.     /**
  71.      * @throws Twig_Error_Loader
  72.      * @throws Twig_Error_Runtime
  73.      * @throws Twig_Error_Syntax
  74.      * @throws \Exception
  75.      * @throws GuzzleException
  76.      */
  77.     public function indexAction(Request $requestParameterBagInterface $parameterBag): Response
  78.     {
  79.         if (
  80.             $this->featureManager->isActive(FeatureFlagListener::FEATURE_MOTION_RECRUIT) &&
  81.             $username $parameterBag->get('site_motion_recruit_username')
  82.         ) {
  83.             return $this->redirect(
  84.                 MotionRecruitService::generateJobAlertUrl($username)
  85.             );
  86.         }
  87.         $jobAlert = new JobAlert();
  88.         $form $this->createForm(JobAlertType::class, $jobAlert, [
  89.             'is_expanded' => true,
  90.         ]);
  91.         if ($this->featureManager->isActive(FeatureFlagListener::FEATURE_CHILD_SITE)) {
  92.             $form $this->jobAlertService->getJobAlertForm(
  93.                 $jobAlert,
  94.                 ['attr' => ['class' => 'recaptcha-form']],
  95.                 $request->getHost()
  96.             );
  97.         }
  98.         $form->handleRequest($request);
  99.         if ($form->isSubmitted()) {
  100.             if ($form->isValid()) {
  101.                 /* @var Coordinates $coordinates */
  102.                 if ($jobAlert->getLocation()) {
  103.                     $coordinates $this->geocode->getCoordinatesByLocation($jobAlert->getLocation());
  104.                     $jobAlert->setLatitude($coordinates->getLatitude())
  105.                         ->setLongitude($coordinates->getLongitude())
  106.                     ;
  107.                 }
  108.                 $jobAlert->setLocale($request->getLocale());
  109.                 if (!empty($this->config->get('site_jobalert_default_interval'))) {
  110.                     $jobAlert->setFrequency($this->config->get('site_jobalert_default_interval'));
  111.                 }
  112.                 if ($this->featureManager->isActive(FeatureFlagListener::FEATURE_CHILD_SITE)) {
  113.                     $result $this->jobAlertService->postJobalert($jobAlert$request->getHost());
  114.                     if ($result) {
  115.                         $this->addFlash('success''Jobalert is created');
  116.                         return $this->redirectToRoute('jobalert_thanks');
  117.                     }
  118.                 } else {
  119.                     $this->entityManager->persist($jobAlert);
  120.                     $this->entityManager->flush();
  121.                     $this->eventDispatcher->dispatch(new JobAlertCreatedEvent($jobAlert));
  122.                     $this->addFlash('success''Jobalert is created');
  123.                     return $this->redirectToRoute('jobalert_thanks');
  124.                 }
  125.             }
  126.             $this->addFlash('danger''Cannot create jobalert');
  127.         }
  128.         $response $this
  129.             ->pageRenderer
  130.             ->renderPage(
  131.                 '{jobalert_form}',
  132.                 '@default/pages/jobalert.html.twig',
  133.                 [
  134.                     'jobalert' => $jobAlert,
  135.                     'form' => $form->createView(),
  136.                     'show_privacy_statement_text' => (JobAlertSetting::PRIVACY_STATEMENT_STRATEGY_TEXT
  137.                         === $this->config->get('site_jobalert_privacy_statement_option')),
  138.                     'privacy_statement_text' => $this->config->get('site_jobalert_privacy_statement_text'),
  139.                 ]
  140.             );
  141.         $response->setSharedMaxAge(0);
  142.         return $response;
  143.     }
  144.     /**
  145.      * @throws \Exception
  146.      * @throws GuzzleException
  147.      */
  148.     #[Route(path'/modal/jobalert'name'jobalert_create_modal')]
  149.     public function indexModalAction(Request $request): Response
  150.     {
  151.         $jobAlert = new JobAlert();
  152.         $form $this->createForm(JobAlertType::class, $jobAlert, [
  153.             'is_expanded' => true,
  154.         ]);
  155.         if ($this->featureManager->isActive(FeatureFlagListener::FEATURE_CHILD_SITE)) {
  156.             $form $this->jobAlertService->getJobAlertForm($jobAlert, [], $request->getHost());
  157.         }
  158.         $form->handleRequest($request);
  159.         if ($form->isSubmitted()) {
  160.             if ($form->isValid()) {
  161.                 /* @var Coordinates $coordinates */
  162.                 if ($jobAlert->getLocation()) {
  163.                     $coordinates $this->geocode->getCoordinatesByLocation($jobAlert->getLocation());
  164.                     $jobAlert->setLatitude($coordinates->getLatitude())
  165.                         ->setLongitude($coordinates->getLongitude())
  166.                     ;
  167.                 }
  168.                 if (!empty($this->config->get('site_jobalert_default_interval'))) {
  169.                     $jobAlert->setFrequency($this->config->get('site_jobalert_default_interval'));
  170.                 }
  171.                 $jobAlert->setLocale($request->getLocale());
  172.                 if ($this->featureManager->isActive(FeatureFlagListener::FEATURE_CHILD_SITE)) {
  173.                     $result $this->jobAlertService->postJobalert($jobAlert$request->getHost());
  174.                     if ($result) {
  175.                         $this->addFlash('success''Jobalert is created');
  176.                         return $this->redirectToRoute('jobalert_thanks');
  177.                     }
  178.                 } else {
  179.                     $this->entityManager->persist($jobAlert);
  180.                     $this->entityManager->flush();
  181.                     $this->addFlash('success''Jobalert is created');
  182.                     $this->eventDispatcher->dispatch(new JobAlertCreatedEvent($jobAlert));
  183.                     return $this->redirectToRoute('jobalert_thanks');
  184.                 }
  185.             }
  186.             $this->addFlash('danger''Cannot create jobalert');
  187.         }
  188.         $response $this->render(
  189.             '@default/pages/Modal/jobalert_modal.html.twig',
  190.             [
  191.                 'jobalert' => $jobAlert,
  192.                 'form' => $form->createView(),
  193.                 'show_privacy_statement_text' => (JobAlertSetting::PRIVACY_STATEMENT_STRATEGY_TEXT
  194.                     === $this->config->get('site_jobalert_privacy_statement_option')),
  195.                 'privacy_statement_text' => $this->config->get('site_jobalert_privacy_statement_text'),
  196.             ]
  197.         );
  198.         $response->setSharedMaxAge(0);
  199.         return $response;
  200.     }
  201.     /**
  202.      * @throws Twig_Error_Loader
  203.      * @throws Twig_Error_Runtime
  204.      * @throws Twig_Error_Syntax
  205.      */
  206.     public function thanksAction(): Response
  207.     {
  208.         return $this
  209.             ->pageRenderer
  210.             ->renderPage(
  211.                 '{jobalert_thanks}',
  212.                 '@default/pages/jobalert_thanks.html.twig',
  213.                 []
  214.             );
  215.     }
  216.     #[Route(path'/unsubscribe/{hash}'name'jobalert_unsubscribe')]
  217.     public function unsubscribe(string $hash): Response
  218.     {
  219.         $hash json_decode(base64_decode($hashfalse), true);
  220.         if (!\is_array($hash) || !\array_key_exists('email'$hash)) {
  221.             return $this->redirectToRoute('homepage');
  222.         }
  223.         $matchingJobAlertCollection $this->entityManager->getRepository(JobAlert::class)->findBy([
  224.             'email' => $hash['email'],
  225.         ]);
  226.         if (!$matchingJobAlertCollection) {
  227.             return $this->redirectToRoute('homepage');
  228.         }
  229.         foreach ($matchingJobAlertCollection as $matchingJobAlert) {
  230.             $this->entityManager->remove($matchingJobAlert);
  231.         }
  232.         $this->entityManager->flush();
  233.         return $this->render('pages/jobalert/unsubscribe.html.twig', [
  234.             'email' => $hash['email'],
  235.         ]);
  236.     }
  237.     /**
  238.      * Due to time pressure this is not completely DRY - a lot of similarities with companyJobalertAction. Refactor later.
  239.      */
  240.     #[Route(path'/option_value/{id}'name'jobalert_optionvalue_jobalert')]
  241.     public function singleOptionValueJobalert(Request $requestint $id): RedirectResponse|JsonResponse|Response
  242.     {
  243.         $jobalert = new JobAlert();
  244.         $form $this->createForm(CompanyJobAlertType::class, $jobalert, [
  245.             'action' => $this->generateUrl('jobalert_company_jobalert', ['id' => $id'redirect' => $request->query->get('redirect')]),
  246.         ]);
  247.         $form->handleRequest($request);
  248.         $serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);
  249.         $optionValue $serializer->deserialize(json_encode(['id' => $id]), OptionValue::class, 'json');
  250.         if ($form->isSubmitted() && $form->isValid()) {
  251.             $jobalert->setFrequency(JobAlert::FREQUENCY_WEEKLY);
  252.             if ($this->featureManager->isActive(FeatureFlagListener::FEATURE_CHILD_SITE)) {
  253.                 $this->jobAlertService->postJobalert($jobalert$request->getHost(), [$optionValue]);
  254.             } else {
  255.                 $this->entityManager->persist($jobalert);
  256.                 $this->entityManager->flush();
  257.             }
  258.             if ($request->isXmlHttpRequest()) {
  259.                 return new JsonResponse([
  260.                     'message' => $this->localisedSettingService->getLocalisedSetting('site_jobalert_success_message'),
  261.                 ]);
  262.             }
  263.             $this->addFlash('success'$this->localisedSettingService->getLocalisedSetting('site_jobalert_success_message'));
  264.             return new RedirectResponse($request->query->get('redirect'));
  265.         }
  266.         return $this->render('@default/pages/company_job_alert_form.html.twig', ['form' => $form->createView()]);
  267.     }
  268.     /**
  269.      * @throws \Exception
  270.      */
  271.     #[Route(path'/company/{id}'name'jobalert_company_jobalert')]
  272.     public function companyJobalertAction(Request $requestint $id): Response
  273.     {
  274.         if (!$company $this->companyService->getCompanyFromRequest($request)) {
  275.             throw $this->createNotFoundException('Company is not available');
  276.         }
  277.         $jobalert = new JobAlert();
  278.         $form $this->createForm(CompanyJobAlertType::class, $jobalert, [
  279.             'action' => $this->generateUrl('jobalert_company_jobalert', ['id' => $id'redirect' => $request->query->get('redirect')]),
  280.         ]);
  281.         $form->handleRequest($request);
  282.         if ($form->isSubmitted()) {
  283.             if ($form->isValid()) {
  284.                 $jobalert->setCompany($company)
  285.                     ->setFrequency(JobAlert::FREQUENCY_WEEKLY)
  286.                 ;
  287.                 if ($this->featureManager->isActive(FeatureFlagListener::FEATURE_CHILD_SITE)) {
  288.                     $this->jobAlertService->postJobalert($jobalert$request->getHost());
  289.                 } else {
  290.                     $this->entityManager->persist($jobalert);
  291.                     $this->entityManager->flush();
  292.                 }
  293.                 if ($request->isXmlHttpRequest()) {
  294.                     return new JsonResponse([
  295.                         'message' => $this->localisedSettingService->getLocalisedSetting('site_jobalert_success_message'),
  296.                     ]);
  297.                 }
  298.                 $this->addFlash('success'$this->localisedSettingService->getLocalisedSetting('site_jobalert_success_message'));
  299.                 return new RedirectResponse($request->query->get('redirect'));
  300.             }
  301.             $this->addFlash('danger'$this->translator->trans('Job alert could not be created'));
  302.         }
  303.         return $this->render('@default/pages/company_job_alert_form.html.twig', ['form' => $form->createView()]);
  304.     }
  305. }