src/App/EventSubscriber/App/UserEventSubscriber.php line 118

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\App;
  3. use App\Entity\AutomatedEmailNotificationDefinition\AutomatedEmailNotificationDefinitionTextContent;
  4. use App\Entity\Notification;
  5. use App\Entity\UsageEvent;
  6. use App\Entity\User;
  7. use App\Event\NewUserEmailAddressConfirmedEvent;
  8. use App\Event\UserAccountConfirmedEvent;
  9. use App\Event\UserEmailAddressEditedEvent;
  10. use App\Event\UserRegisteredEvent;
  11. use App\Event\UserTermsAndConditionsAcceptedEvent;
  12. use App\Event\UserWillBeRemovedEvent;
  13. use App\Service\AutomatedEmailNotificationDefinitionService;
  14. use App\Service\ConversionEventService;
  15. use App\Service\CustomerHelpDeskService;
  16. use App\Service\ExternalFeedsService;
  17. use App\Service\MailService;
  18. use App\Service\OnetimeLoginTokenService;
  19. use App\Service\RouterHelperService;
  20. use App\Service\UsageEventService;
  21. use App\Service\UserService;
  22. use App\SymfonyMessage\SendJoboffererMissingMembershipAfterRegistrationMailSymfonyMessage;
  23. use App\Utility\DateTimeUtility;
  24. use Doctrine\ORM\EntityManagerInterface;
  25. use Exception;
  26. use JanusHercules\DatawarehouseIntegration\Domain\Entity\BusinessEvent;
  27. use JanusHercules\DatawarehouseIntegration\Domain\Entity\ExternalPartnerEvent;
  28. use JanusHercules\DatawarehouseIntegration\Domain\Service\BusinessEventDomainService;
  29. use JanusHercules\DatawarehouseIntegration\Domain\Service\ExternalPartnerEventDomainService;
  30. use Psr\Log\LoggerInterface;
  31. use Swift_Message;
  32. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  33. use Symfony\Component\Messenger\MessageBusInterface;
  34. use Symfony\Component\Messenger\Stamp\DelayStamp;
  35. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  36. use Symfony\Contracts\Translation\TranslatorInterface;
  37. use Throwable;
  38. use Twig\Environment;
  39. use Twig\Error\LoaderError;
  40. use Twig\Error\RuntimeError;
  41. use Twig\Error\SyntaxError;
  42. class UserEventSubscriber implements EventSubscriberInterface
  43. {
  44. public function __construct(
  45. private BusinessEventDomainService $businessEventDomainService,
  46. private ConversionEventService $conversionEventService,
  47. private ExternalPartnerEventDomainService $externalPartnerEventDomainService,
  48. private UsageEventService $usageEventService,
  49. private RouterHelperService $routerHelperService,
  50. private OnetimeLoginTokenService $onetimeLoginTokenService,
  51. private EntityManagerInterface $entityManager,
  52. private TranslatorInterface $translator,
  53. private MailService $mailService,
  54. private AutomatedEmailNotificationDefinitionService $automatedEmailNotificationDefinitionService,
  55. private Environment $twigEnvironment,
  56. private LoggerInterface $logger,
  57. private UserService $userService,
  58. private CustomerHelpDeskService $customerHelpDeskService,
  59. private readonly MessageBusInterface $messageBus
  60. ) {
  61. }
  62. public static function getSubscribedEvents(): array
  63. {
  64. return [
  65. UserRegisteredEvent::class => 'onUserRegistered',
  66. UserWillBeRemovedEvent::class => 'onUserWillBeRemoved',
  67. UserAccountConfirmedEvent::class => 'onUserAccountConfirmed',
  68. UserEmailAddressEditedEvent::class => 'onUserEmailAddressEdited',
  69. NewUserEmailAddressConfirmedEvent::class => 'onNewUserEmailAddressConfirmed',
  70. UserTermsAndConditionsAcceptedEvent::class => 'onUserTermsAndConditionsAccepted'
  71. ];
  72. }
  73. public function onUserTermsAndConditionsAccepted(
  74. UserTermsAndConditionsAcceptedEvent $event
  75. ): void {
  76. $user = $event->getUser();
  77. $user->setAcceptedTermsAndConditions(true);
  78. $this->entityManager->persist($user);
  79. $this->entityManager->flush();
  80. $this->businessEventDomainService->writeNewEvent(
  81. BusinessEvent::EVENT_TYPE_TERMS_AND_CONDITIONS_HAS_BEEN_ACCEPTED,
  82. $event->getUser(),
  83. $event->getRecurrentJob()
  84. );
  85. }
  86. public function onNewUserEmailAddressConfirmed(
  87. NewUserEmailAddressConfirmedEvent $event): void
  88. {
  89. $user = $event->getUser();
  90. $this->customerHelpDeskService->userEmailAddressHasChanged($user, $event->getConfirmedUserEmailAddress());
  91. $this->logger->info(
  92. "About to handle NewUserEmailAddressConfirmed for user {$user->getId()}"
  93. );
  94. $this->businessEventDomainService->writeNewEvent(BusinessEvent::EVENT_TYPE_NEW_USER_EMAIL_ADDRESS_CONFIRMED, $user);
  95. }
  96. public function onUserEmailAddressEdited(
  97. UserEmailAddressEditedEvent $event
  98. ): void {
  99. $user = $event->getUser();
  100. $this->userService->sendConfirmationEmailToUser($user, $event->getEditedUserEmailAddress());
  101. $this->logger->info(
  102. "About to handle UserEmailAddressEditedEvent for user {$user->getId()}"
  103. );
  104. }
  105. /** @throws Exception */
  106. public function onUserRegistered(
  107. UserRegisteredEvent $event
  108. ): void {
  109. $this->logger->info(
  110. "About to handle UserRegisteredEvent for user {$event->getUser()->getId()}"
  111. );
  112. try {
  113. $user = $event->getUser();
  114. $extendedApplication = $event->getExtendedApplication();
  115. $this->businessEventDomainService->writeNewEvent(
  116. BusinessEvent::EVENT_TYPE_NEW_USER_ACCOUNT_REGISTERED,
  117. $user,
  118. null,
  119. null,
  120. json_encode(['userCreatedVia' => $user->getCreatedVia()])
  121. );
  122. $additionalData = json_encode(['createdVia' => 'normal_reg']);
  123. if ($event->getUser()->getCreatedVia() === User::CREATED_VIA_GOOGLE_IDENTITY_OAUTH2) {
  124. $this->businessEventDomainService->writeNewEvent(
  125. BusinessEvent::EVENT_TYPE_NEW_USER_ACCOUNT_REGISTERED_VIA_GOOGLE_IDENTITY_OAUTH2,
  126. $user,
  127. $extendedApplication?->getRecurrentJob()
  128. );
  129. $additionalData = json_encode(['createdVia' => 'google_identity_oauth2']);
  130. } elseif ($event->getUser()->getCreatedVia() === User::CREATED_VIA_GOOGLE_IDENTITY_GSI) {
  131. $this->businessEventDomainService->writeNewEvent(
  132. BusinessEvent::EVENT_TYPE_NEW_USER_ACCOUNT_REGISTERED_VIA_GOOGLE_IDENTITY_GSI,
  133. $user,
  134. $extendedApplication?->getRecurrentJob()
  135. );
  136. $additionalData = json_encode(['createdVia' => 'google_identity_gsi']);
  137. } elseif ($event->getUser()->getCreatedVia() === User::CREATED_VIA_WANTED_JOBS_SEARCH_ANONYMOUS_USER_FORM) {
  138. $additionalData = json_encode(['createdVia' => 'wanted_jobs_search_anonymous_form']);
  139. }
  140. $this->conversionEventService->handleConversionGoalReached(
  141. ConversionEventService::CAMPAIGN_ID_REGISTRATION_REFERER,
  142. $user->isJobseeker() ? 1 : 0,
  143. null,
  144. $user,
  145. $extendedApplication?->getRecurrentJob(),
  146. null,
  147. $additionalData
  148. );
  149. foreach ([
  150. ConversionEventService::CAMPAIGN_ID_SHARED_RECURRENT_JOBS,
  151. ConversionEventService::CAMPAIGN_ID_RECURRENT_JOBS_TO_PRINT_QR_CODE,
  152. ConversionEventService::CAMPAIGN_ID_INSTAGRAM,
  153. ConversionEventService::CAMPAIGN_ID_EASY_LEBENSLAUF,
  154. ConversionEventService::CAMPAIGN_ID_JOBWARE,
  155. ConversionEventService::CAMPAIGN_ID_AGENTUR_FUER_ARBEIT,
  156. ConversionEventService::CAMPAIGN_ID_SIMILAR_JOBS,
  157. ConversionEventService::CAMPAIGN_ID_GOOGLE_FOR_JOBS,
  158. ConversionEventService::CAMPAIGN_ID_EXTENDED_APPLICATION_INFO_MAIL_NONSUBSCRIBED_JOBOFFERERS,
  159. ConversionEventService::CAMPAIGN_ID_EMAIL_MARKETING_AKTION,
  160. ConversionEventService::CAMPAIGN_ID_EMAIL_MARKETING_SONDERAKTION,
  161. ConversionEventService::CAMPAIGN_ID_LINKED_IN,
  162. ConversionEventService::CAMPAIGN_ID_PERSPECTIVE,
  163. ConversionEventService::CAMPAIGN_ID_XING_FEED
  164. ] as $campaignId
  165. ) {
  166. $this->conversionEventService->handleConversionGoalReached(
  167. $campaignId,
  168. 0,
  169. null,
  170. $user,
  171. $extendedApplication?->getRecurrentJob()
  172. );
  173. }
  174. foreach ([
  175. ConversionEventService::CAMPAIGN_ID_EMAIL_MARKETING_ANGEBOT,
  176. ConversionEventService::CAMPAIGN_ID_EMAIL_MARKETING_SONDERANGEBOT,
  177. ConversionEventService::CAMPAIGN_ID_EMAIL_MARKETING_JETZT,
  178. ConversionEventService::CAMPAIGN_ID_YOUTUBE
  179. ] as $campaignId
  180. ) {
  181. $this->conversionEventService->handleConversionGoalReached(
  182. $campaignId,
  183. 0,
  184. null,
  185. $user
  186. );
  187. }
  188. foreach ([
  189. ConversionEventService::CAMPAIGN_ID_JOBLIFT,
  190. ConversionEventService::CAMPAIGN_ID_TALENTCOM,
  191. ConversionEventService::CAMPAIGN_ID_STELLENONLINE,
  192. ConversionEventService::CAMPAIGN_ID_CRITEO,
  193. ConversionEventService::CAMPAIGN_ID_JOOBLE,
  194. ConversionEventService::CAMPAIGN_ID_STEPSTONE_FEED
  195. ] as $campaignId
  196. ) {
  197. foreach ([0, 1, 2] as $subcampaignId) {
  198. $this->conversionEventService->handleConversionGoalReached(
  199. $campaignId,
  200. $subcampaignId,
  201. null,
  202. $user,
  203. $extendedApplication?->getRecurrentJob()
  204. );
  205. }
  206. }
  207. foreach ([
  208. ConversionEventService::CAMPAIGN_ID_BING_LANDINGPAGES,
  209. ConversionEventService::CAMPAIGN_ID_JOYN
  210. ] as $campaignId
  211. ) {
  212. foreach ([0, 1, 2, 3] as $subcampaignId) {
  213. $this->conversionEventService->handleConversionGoalReached(
  214. $campaignId,
  215. $subcampaignId,
  216. null,
  217. $user,
  218. $extendedApplication?->getRecurrentJob()
  219. );
  220. }
  221. }
  222. foreach ([
  223. ConversionEventService::CAMPAIGN_ID_JOBIJOBA,
  224. ConversionEventService::CAMPAIGN_ID_WHATJOBS
  225. ] as $campaignId
  226. ) {
  227. foreach ([0, 1, 2, 3, 4, 5] as $subcampaignId) {
  228. $this->conversionEventService->handleConversionGoalReached(
  229. $campaignId,
  230. $subcampaignId,
  231. null,
  232. $user,
  233. $extendedApplication?->getRecurrentJob()
  234. );
  235. }
  236. }
  237. foreach ([0, 1, 2, 3, 4, 5, 7] as $subcampaignId) {
  238. $this->conversionEventService->handleConversionGoalReached(
  239. ConversionEventService::CAMPAIGN_ID_RECURRENT_JOBS_SEARCH_FOR_ONE_SPECIFIC_CUSTOMER,
  240. $subcampaignId,
  241. null,
  242. $user,
  243. $extendedApplication?->getRecurrentJob(),
  244. null,
  245. null,
  246. !is_null($user)
  247. ? $user->getIntegratedExternalPartnerCustomer()
  248. : null
  249. );
  250. }
  251. foreach ([4, 5, 6] as $subcampaignId) {
  252. if ($subcampaignId !== 5) {
  253. $this->conversionEventService->handleConversionGoalReached(
  254. ConversionEventService::CAMPAIGN_ID_TALENTCOM,
  255. $subcampaignId,
  256. null,
  257. $user,
  258. $extendedApplication?->getRecurrentJob()
  259. );
  260. }
  261. $this->conversionEventService->handleConversionGoalReached(
  262. ConversionEventService::CAMPAIGN_ID_STELLENONLINE,
  263. $subcampaignId,
  264. null,
  265. $user,
  266. $extendedApplication?->getRecurrentJob()
  267. );
  268. $this->conversionEventService->handleConversionGoalReached(
  269. ConversionEventService::CAMPAIGN_ID_JOBLIFT,
  270. $subcampaignId,
  271. null,
  272. $user,
  273. $extendedApplication?->getRecurrentJob()
  274. );
  275. }
  276. foreach (ExternalFeedsService::EBAY_SUBCAMPAIGN_VARIANTS as $subcampaignId) {
  277. $this->conversionEventService->handleConversionGoalReached(
  278. ConversionEventService::CAMPAIGN_ID_EBAY,
  279. $subcampaignId,
  280. null,
  281. $user,
  282. $extendedApplication?->getRecurrentJob()
  283. );
  284. }
  285. foreach (range(0, 500) as $subcampaignId) {
  286. $this->conversionEventService->handleConversionGoalReached(
  287. ConversionEventService::CAMPAIGN_ID_FACEBOOK,
  288. $subcampaignId,
  289. null,
  290. $user,
  291. $extendedApplication?->getRecurrentJob()
  292. );
  293. }
  294. foreach ([501, 503] as $subcampaignId) {
  295. $this->conversionEventService->handleConversionGoalReached(
  296. ConversionEventService::CAMPAIGN_ID_FACEBOOK,
  297. $subcampaignId,
  298. null,
  299. $user,
  300. $extendedApplication?->getRecurrentJob()
  301. );
  302. }
  303. $this->conversionEventService->handleConversionGoalReached(
  304. ConversionEventService::CAMPAIGN_ID_EASY_LEBENSLAUF,
  305. 1,
  306. null,
  307. $user,
  308. $extendedApplication?->getRecurrentJob()
  309. );
  310. foreach (range(0, 31) as $subcampaignId) {
  311. $this->conversionEventService->handleConversionGoalReached(
  312. ConversionEventService::CAMPAIGN_ID_LANDINGPAGES,
  313. $subcampaignId,
  314. null,
  315. $user,
  316. $extendedApplication?->getRecurrentJob()
  317. );
  318. }
  319. foreach ([0, 1] as $subcampaignId) {
  320. $this->conversionEventService->handleConversionGoalReached(
  321. ConversionEventService::CAMPAIGN_ID_GMX,
  322. $subcampaignId,
  323. null,
  324. $user,
  325. $extendedApplication?->getRecurrentJob()
  326. );
  327. }
  328. foreach ([0, 1, 2, 7, 8, 9, 10, 11, 12, 13] as $subcampaignId) {
  329. $this->conversionEventService->handleConversionGoalReached(
  330. ConversionEventService::CAMPAIGN_ID_MEINESTADT,
  331. $subcampaignId,
  332. null,
  333. $user,
  334. $extendedApplication?->getRecurrentJob()
  335. );
  336. }
  337. if ($event->getUser()->isJobofferer()) {
  338. $this->conversionEventService->handleConversionGoalReached(
  339. ConversionEventService::CAMPAIGN_ID_EBAY,
  340. 8,
  341. null,
  342. $user,
  343. $extendedApplication?->getRecurrentJob()
  344. );
  345. // Marketing-Landingpages AG-Registrierung (pages.joboo.de)
  346. // Loop über alle möglichen Subcampaign-IDs (0-1000)
  347. for ($subcampaignId = 0; $subcampaignId <= 1000; ++$subcampaignId) {
  348. $this->conversionEventService->handleConversionGoalReached(
  349. ConversionEventService::CAMPAIGN_ID_DIGITAL_ESTATE_MARKETING_LANDINGPAGES_AG_REGISTRATION,
  350. $subcampaignId,
  351. null,
  352. $user,
  353. $extendedApplication?->getRecurrentJob()
  354. );
  355. }
  356. }
  357. if ($event->getUser()->isJobseeker()) {
  358. $this->conversionEventService->handleConversionGoalReached(
  359. ConversionEventService::CAMPAIGN_ID_UGC_FUNNEL,
  360. 0,
  361. null,
  362. $user,
  363. $extendedApplication?->getRecurrentJob()
  364. );
  365. if ($extendedApplication) {
  366. foreach ([0, 1] as $subcampaignId) {
  367. $this->conversionEventService->handleConversionGoalReached(
  368. ConversionEventService::CAMPAIGN_ID_PERSOMATCH_FEED,
  369. $subcampaignId,
  370. null,
  371. $user,
  372. $extendedApplication->getRecurrentJob()
  373. );
  374. }
  375. }
  376. }
  377. } catch (Throwable $t) {
  378. $this->logger->error("Error handling UserRegisteredEvent for user {$event->getUser()->getId()}: {$t->getMessage()}");
  379. }
  380. }
  381. public function onUserWillBeRemoved(UserWillBeRemovedEvent $event)
  382. {
  383. $user = $event->getUser();
  384. if ($user->isLinkedToExternalPartner()) {
  385. $this->externalPartnerEventDomainService->writeEvent(
  386. ExternalPartnerEvent::EVENT_TYPE_USER_REMOVED,
  387. $user->getExternalPartner(),
  388. null,
  389. null,
  390. null,
  391. null,
  392. null,
  393. null,
  394. $user
  395. );
  396. }
  397. }
  398. /**
  399. * @throws RuntimeError
  400. * @throws SyntaxError
  401. * @throws LoaderError
  402. * @throws Exception|\Doctrine\DBAL\Driver\Exception
  403. */
  404. public function onUserAccountConfirmed(UserAccountConfirmedEvent $event): void
  405. {
  406. $user = $event->getUser();
  407. $this->businessEventDomainService->writeNewEvent(BusinessEvent::EVENT_TYPE_NEW_USER_ACCOUNT_CONFIRMED, $user);
  408. $this->usageEventService->eventHasOccurredForUser($user, UsageEvent::EVENT_TYPE_NEW_USER_HAS_CONFIRMED_ACCOUNT);
  409. $replacements = [
  410. 'contactUrl' => $this->routerHelperService->generate('contact', [], UrlGeneratorInterface::ABSOLUTE_URL),
  411. 'resetUrl' => $this->routerHelperService->generate(
  412. 'fos_user_resetting_request',
  413. [],
  414. UrlGeneratorInterface::ABSOLUTE_URL
  415. )
  416. ];
  417. $bodyId = 0;
  418. if ($user->isJobofferer()) {
  419. $bodyId = AutomatedEmailNotificationDefinitionService::BODY_ID_WELCOME_AFTER_CONFIRMED_BODY_JOBOFFERER;
  420. }
  421. if ($user->isJobseeker()) {
  422. $bodyId = AutomatedEmailNotificationDefinitionService::BODY_ID_WELCOME_AFTER_CONFIRMED_BODY_JOBSEEKER;
  423. }
  424. /** @var AutomatedEmailNotificationDefinitionTextContent $textContent */
  425. $textContent = $this
  426. ->entityManager
  427. ->getRepository(AutomatedEmailNotificationDefinitionTextContent::class)
  428. ->findOneBy([
  429. 'automatedEmailNotificationDefinition' => MailService::MAIL_ID_WELCOME_AFTER_CONFIRMED,
  430. 'subjectId' => AutomatedEmailNotificationDefinitionService::SUBJECT_ID_WELCOME_AFTER_CONFIRMED,
  431. 'bodyId' => $bodyId
  432. ]);
  433. $replacements['confirmationUrl'] = $this->onetimeLoginTokenService->generateOneTimeLoginUrl(
  434. OnetimeLoginTokenService::REDIRECT_KEY_SETOWNPASSWORD_FORM,
  435. $user,
  436. [],
  437. ConversionEventService::CAMPAIGN_ID_WELCOME_MAIL,
  438. $user->isJobseeker() ? 0 : 1,
  439. null,
  440. $event->getRequest(),
  441. null,
  442. UrlGeneratorInterface::ABSOLUTE_URL,
  443. false,
  444. $textContent->getAutomatedEmailNotificationDefinition()->getId(),
  445. $textContent->getBodyId()
  446. );
  447. $message = (new Swift_Message($textContent->getSubject()))
  448. ->setFrom(MailService::DEFAULT_SENDER_NOREPLY_MAILADDRESS, $this->translator->trans('direct_email_communication.outgoing_message.noreply_sender_name'))
  449. ->setTo($user->getEmail())
  450. ->setBody(
  451. $this->twigEnvironment->render(
  452. $textContent->getTwigTemplateName(),
  453. $replacements
  454. ),
  455. 'text/html'
  456. );
  457. $message->setFrom('team@joboo.de', $this->translator->trans('mailfromname.team'));
  458. if ($user->isJobofferer()) {
  459. $message->setBcc('vertragsdoku@joboo.de');
  460. }
  461. $this->mailService->queueForSending(MailService::MAIL_ID_WELCOME_AFTER_CONFIRMED, $message, $user);
  462. if ($user->isJobofferer()) {
  463. $this->messageBus->dispatch(
  464. new SendJoboffererMissingMembershipAfterRegistrationMailSymfonyMessage($user->getId()),
  465. [DelayStamp::delayUntil(
  466. DateTimeUtility::createDateTimeUtc('+ 120 minutes')
  467. )]
  468. );
  469. }
  470. $this
  471. ->automatedEmailNotificationDefinitionService
  472. ->dispatchEmailNotificationTrigger(
  473. Notification::EMAIL_NOTIFICATION_TRIGGER_POINT_ACCOUNT_CONFIRMATION,
  474. $user
  475. );
  476. }
  477. }