<commit_before><?php
/**
 * DoubleOptInStrategy.php
 *
 * @since 21/09/16
 * @author gseidel
 */

namespace Enhavo\Bundle\NewsletterBundle\Strategy;

use Enhavo\Bundle\NewsletterBundle\Form\Resolver;
use Enhavo\Bundle\NewsletterBundle\Model\SubscriberInterface;
use Enhavo\Bundle\NewsletterBundle\Storage\LocalStorage;
use Enhavo\Bundle\NewsletterBundle\Storage\StorageResolver;
use Enhavo\Bundle\NewsletterBundle\Storage\StorageInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class DoubleOptInStrategy extends AbstractStrategy
{
    /**
     * @var LocalStorage
     */
    private $localStorage;

    /**
     * @var StorageResolver
     */
    private $storageResolver;

    /**
     * @var Resolver
     */
    private $formResolver;

    /**
     * DoubleOptInStrategy constructor.
     * @param array$options
     * @param array $typeOptions
     * @param LocalStorage $localStorage
     * @param StorageResolver $storageResolver
     * @param Resolver $formResolver
     */
    public function __construct($options, $typeOptions, LocalStorage $localStorage, StorageResolver $storageResolver, Resolver $formResolver)
    {
        parent::__construct($options, $typeOptions);
        $this->localStorage = $localStorage;
        $this->storageResolver = $storageResolver;
        $this->formResolver = $formResolver;
    }

    public function addSubscriber(SubscriberInterface $subscriber)
    {
        $subscriber->setCreatedAt(new \DateTime());
        $subscriber->setActive(false);
        $this->setToken($subscriber);
        $this->localStorage->saveSubscriber($subscriber);
        $this->notifySubscriber($subscriber, $subscriber->getType());
        return 'subscriber.form.message.double_opt_in';
    }

    public function activateSubscriber(SubscriberInterface $subscriber, $type)
    {
        $subscriber->setActive(true);
        $subscriber->setToken(null);
        $this->localStorage->saveSubscriber($subscriber);
        $this->getSubscriberManager()->saveSubscriber($subscriber, $type);
        $this->notifyAdmin($subscriber, $type);
        $this->confirmSubscriber($subscriber, $subscriber->getType());
    }

    private function notifySubscriber(SubscriberInterface $subscriber, $type)
    {
        $notify = $this->getTypeOption('notify', $type, true);
        if($notify) {
            $link = $this->getRouter()->generate('enhavo_newsletter_subscribe_activate', [
                'token' => $subscriber->getToken(),
                'type' => $subscriber->getType()
            ], UrlGeneratorInterface::ABSOLUTE_URL);

            $template = $this->getTypeOption('template', $type, 'EnhavoNewsletterBundle:Subscriber:Email/double-opt-in.html.twig');
            $from = $this->getTypeOption('from', $type, '<EMAIL>');
            $senderName = $this->getTypeOption('sender_name', $type, 'enahvo');

            $message = new \Swift_Message();
            $message->setSubject($this->getSubject())
                ->setFrom($from, $senderName)
                ->setTo($subscriber->getEmail())
                ->setBody($this->renderTemplate($template, [
                    'subscriber' => $subscriber,
                    'link' => $link
                ]), 'text/html');

            $this->sendMessage($message);
        }
    }

    private function confirmSubscriber(SubscriberInterface $subscriber, $type)
    {
        $notify = $this->getTypeOption('confirm', $type, true);
        if($notify) {

            $template = $this->getTypeOption('confirmation_template', $type, 'EnhavoNewsletterBundle:Subscriber:Email/confirmation.html.twig');
            $from = $this->getTypeOption('from', $type, '<EMAIL>');
            $senderName = $this->getTypeOption('sender_name', $type, 'enahvo');

            $message = new \Swift_Message();<commit_msg>comment on todo<commit_after><?php
/**
 * DoubleOptInStrategy.php
 *
 * @since 21/09/16
 * @author gseidel
 */

namespace Enhavo\Bundle\NewsletterBundle\Strategy;

use Enhavo\Bundle\NewsletterBundle\Form\Resolver;
use Enhavo\Bundle\NewsletterBundle\Model\SubscriberInterface;
use Enhavo\Bundle\NewsletterBundle\Storage\LocalStorage;
use Enhavo\Bundle\NewsletterBundle\Storage\StorageResolver;
use Enhavo\Bundle\NewsletterBundle\Storage\StorageInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class DoubleOptInStrategy extends AbstractStrategy
{
    /**
     * @var LocalStorage
     */
    private $localStorage;

    /**
     * @var StorageResolver
     */
    private $storageResolver;

    /**
     * @var Resolver
     */
    private $formResolver;

    /**
     * DoubleOptInStrategy constructor.
     * @param array$options
     * @param array $typeOptions
     * @param LocalStorage $localStorage
     * @param StorageResolver $storageResolver
     * @param Resolver $formResolver
     */
    public function __construct($options, $typeOptions, LocalStorage $localStorage, StorageResolver $storageResolver, Resolver $formResolver)
    {
        parent::__construct($options, $typeOptions);
        $this->localStorage = $localStorage;
        $this->storageResolver = $storageResolver;
        $this->formResolver = $formResolver;
    }

    public function addSubscriber(SubscriberInterface $subscriber)
    {
        $subscriber->setCreatedAt(new \DateTime());
        $subscriber->setActive(false);
        $this->setToken($subscriber);
        $this->localStorage->saveSubscriber($subscriber);
        $this->notifySubscriber($subscriber, $subscriber->getType());
        return 'subscriber.form.message.double_opt_in';
    }

    public function activateSubscriber(SubscriberInterface $subscriber, $type)
    {
        $subscriber->setActive(true);
        $subscriber->setToken(null);
        $this->localStorage->saveSubscriber($subscriber);
        $this->getSubscriberManager()->saveSubscriber($subscriber, $type);
        $this->notifyAdmin($subscriber, $type);
        $this->confirmSubscriber($subscriber, $subscriber->getType());
    }

    private function notifySubscriber(SubscriberInterface $subscriber, $type)
    {
        $notify = $this->getTypeOption('notify', $type, true);
        if($notify) {
            $link = $this->getRouter()->generate('enhavo_newsletter_subscribe_activate', [
                'token' => $subscriber->getToken(),
                'type' => $subscriber->getType()
            ], UrlGeneratorInterface::ABSOLUTE_URL);

            $template = $this->getTypeOption('template', $type, 'EnhavoNewsletterBundle:Subscriber:Email/double-opt-in.html.twig');
            $from = $this->getTypeOption('from', $type, '<EMAIL>');
            $senderName = $this->getTypeOption('sender_name', $type, 'enahvo');

            $message = new \Swift_Message();
            $message->setSubject($this->getSubject())
                ->setFrom($from, $senderName)
                ->setTo($subscriber->getEmail())
                ->setBody($this->renderTemplate($template, [
                    'subscriber' => $subscriber,
                    'link' => $link
                ]), 'text/html');

            $this->sendMessage($message);
        }
    }

    private function confirmSubscriber(SubscriberInterface $subscriber, $type)
    {
        $notify = $this->getTypeOption('confirm', $type, true);
        if($notify) {
            // TODO add unsubscribe/change subscription link
            $template = $this->getTypeOption('confirmation_template', $type, 'EnhavoNewsletterBundle:Subscriber:Email/confirmation.html.twig');
            $from = $this->getTypeOption('from', $type, '<EMAIL>');
            $senderName = $this->getTypeOption('sender_name', $type, 'enahvo');

            $message = new \Swift_Message();
{'fraction_non_alphanumeric': 0.13723708127759024, 'fraction_numerical': 0.0015580368735393405, 'mean_word_length': 3.0735060814383925, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4333401', 'n_tokens_mistral': 2143, 'n_tokens_neox': 2113, 'n_words': 336}