/**
  * @param RecipientEntity   $recipient
  * @param Subscription|null $subscription
  * @param MailingList|null  $mailingList
  *
  * @return string
  * @SuppressWarnings(PHPMD.CamelCaseVariableName)
  */
 protected function generateSubscriptionSubscribeConfirmLink(RecipientEntity $recipient, Subscription $subscription = null, MailingList $mailingList = null)
 {
     if ($subscription) {
         return '';
     }
     global $TL_LANG, $container;
     $eventDispatcher = $container['event-dispatcher'];
     $title = $mailingList ? $TL_LANG['orm_avisota_recipient']['subscribe_confirmed'] : $TL_LANG['orm_avisota_recipient']['subscribe_globally_confirmed'];
     $event = new GenerateHtmlEvent('copychilds.gif', $title, sprintf('title="%s"', specialchars($title)));
     $eventDispatcher->dispatch(ContaoEvents::IMAGE_GET_HTML, $event);
     $icon = $event->getHtml();
     return sprintf(' <a href="contao/main.php?do=avisota_recipients' . '&act=subscribe-confirmed&recipient=%s&mailing-list=%s&ref=%s">%s</a>', $recipient->getId(), $mailingList ? $mailingList->getId() : 'global', defined('TL_REFERER_ID') ? TL_REFERER_ID : '', $icon);
 }
 /**
  * @param EnvironmentInterface $environment
  * @param                      $migrationSettings
  * @param                      $migrationId
  *
  * @return null|string
  * @SuppressWarnings(PHPMD.CamelCaseVariableName)
  * @SuppressWarnings(PHPMD.LongVariable)
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 protected function generateResponse(EnvironmentInterface $environment, $migrationSettings, $migrationId)
 {
     global $container;
     $eventDispatcher = $container['event-dispatcher'];
     $input = $environment->getInputProvider();
     $translator = $environment->getTranslator();
     $entityManager = EntityHelper::getEntityManager();
     $recipientRepository = EntityHelper::getRepository('Avisota\\Contao:Recipient');
     list($offset, $skipped, $migrated, $channelMailingListMapping, $statement) = $this->getMigrationStatement($migrationSettings);
     /** @var SubscriptionManager $subscriptionManager */
     $subscriptionManager = $container['avisota.subscription'];
     $subscribeOptions = 0;
     if ($migrationSettings['ignoreBlacklist']) {
         $subscribeOptions |= SubscriptionManager::OPT_IGNORE_BLACKLIST;
     }
     $user = \BackendUser::getInstance();
     $response = new StringBuilder();
     $this->addHeaderContent($response, $translator);
     $contaoRecipients = $statement->fetchAll();
     $lastNewMigrationRecipient = (array) \Session::getInstance()->get('AVISOTA_LAST_MIGRATION_RECIPIENT' . $input->getParameter('migration'));
     foreach ($contaoRecipients as $contaoRecipientData) {
         $recipient = $recipientRepository->findOneBy(array('email' => $contaoRecipientData['email']));
         if ($recipient && in_array($recipient->getEmail(), $lastNewMigrationRecipient)) {
             $migrationSettings['overwrite'] = true;
         }
         if (!$recipient) {
             $this->addCreateRecipientInformation($response, $contaoRecipientData, $translator);
             $recipient = new Recipient();
             $recipient->setEmail($contaoRecipientData['email']);
             $recipient->setAddedById($user->id);
             $recipient->setAddedByName($user->name);
             $recipient->setAddedByUsername($user->username);
         } else {
             if (!$migrationSettings['overwrite']) {
                 $skipped++;
                 continue;
             } else {
                 $this->addSkippedRecipientInformation($response, $contaoRecipientData, $translator);
             }
         }
         $mailingList = $channelMailingListMapping[$contaoRecipientData['pid']];
         if (!$mailingList) {
             // graceful ignore missing mailing lists
             $skipped++;
             continue;
         }
         $migrateRecipientEvent = new MigrateRecipientEvent($migrationSettings, $contaoRecipientData, $recipient);
         $eventDispatcher->dispatch(RecipientEvents::MIGRATE_RECIPIENT, $migrateRecipientEvent);
         if (!$recipient->getId()) {
             $lastNewMigrationRecipient[] = $recipient->getEmail();
         }
         $entityManager->persist($recipient);
         $subscriptionManager->subscribe($recipient, $mailingList, ($contaoRecipientData['active'] ? SubscriptionManager::OPT_ACTIVATE : 0) | $subscribeOptions);
         $migrated++;
     }
     $entityManager->flush();
     if (count($contaoRecipients) < 10) {
         \Session::getInstance()->set('AVISOTA_LAST_MIGRATION_RECIPIENT' . $input->getParameter('migration'), array());
         $this->migrationFinished($migrationId, $migrated, $skipped);
         return null;
     } else {
         $offset += count($contaoRecipients);
         $this->updateRedirectSession($migrationId, $offset, $skipped, $migrated, $migrationSettings['channels'], $migrationSettings['overwrite'], $migrationSettings['importFromMembers']);
         $this->addReloadScriptAndButton($response, $translator);
         \Session::getInstance()->set('AVISOTA_LAST_MIGRATION_RECIPIENT' . $input->getParameter('migration'), $lastNewMigrationRecipient);
         return $response->__toString();
     }
 }