<commit_before><?php
/**
 * Created by PhpStorm.
 * User: james
 * Date: 21/07/2014
 * Time: 17:08
 */

namespace IceMarkt\Bundle\MainBundle\Controller;

use IceMarkt\Bundle\MainBundle\Facade\SendFacade;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class MarketingController
 * @package IceMarkt\Bundle\MainBundle\Controller
 */
class MarketingController extends Controller
{
    private $varsForTwig = array();

    /**
     * Method for sending out emails
     *
     * @Route("/marketing/", name="marketing");
     * @Template()
     *
     * @param Request $request
     *
     * @return array
     */
    public function sendEmailsAction(Request $request)
    {
        $sendFacade = new SendFacade(
            $this->getDoctrine()->getManager(),
            $this->get('mailer')
        );

        $em = $this->getDoctrine()->getManager();

        $form = $this->createFormBuilder()
            ->add('email_template_id', 'choice', array(
                'label' => 'Email Template',
                'choices' => $em->getRepository('IceMarktMainBundle:EmailTemplate')->getChoicesArray(),
                'preferred_choices' => array(),
                'attr' => array(
                    'class' => 'form-control'
                )
            ))
            ->add('Send', 'submit', array(
                'attr'   =>  array(
                    'class'   => 'btn btn-primary')
            ))
            ->getForm();

        $this->varsForTwig['sendEmailsForm'] = $form->createView();

        $form->handleRequest($request);

        if ($form->isValid()) {

            $template   = $request->request->get('form');
            $recipients = $em->getRepository('IceMarktMainBundle:MailRecipient')->findAll();

            //TODO send in batches

            foreach ($recipients as $recipient) {
                $sendFacade->sendTemplateToRecipient($template['email_template_id'], $recipient);
            }
        }

        return $this->varsForTwig;
    }
}<commit_msg>added emailEmailBatch action to return json<commit_after><?php
/**
 * Created by PhpStorm.
 * User: james
 * Date: 21/07/2014
 * Time: 17:08
 */

namespace IceMarkt\Bundle\MainBundle\Controller;

use IceMarkt\Bundle\MainBundle\Facade\SendFacade;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * Class MarketingController
 * @package IceMarkt\Bundle\MainBundle\Controller
 */
class MarketingController extends Controller
{
    private $varsForTwig = array();

    /**
     * Method for sending out emails
     *
     * @Route("/marketing/", name="marketing");
     * @Template()
     *
     * @param Request $request
     *
     * @return array
     */
    public function sendEmailsAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();

        $sendFacade = new SendFacade(
            $em,
            $this->get('mailer')
        );

        $form = $this->createFormBuilder()
            ->add('email_template_id', 'choice', array(
                'label' => 'Email Template',
                'choices' => $em->getRepository('IceMarktMainBundle:EmailTemplate')->getChoicesArray(),
                'preferred_choices' => array(),
                'attr' => array(
                    'class' => 'form-control'
                )
            ))
            ->add('Send', 'submit', array(
                'attr'   =>  array(
                    'class'   => 'btn btn-primary')
            ))
            ->getForm();

        $this->varsForTwig['sendEmailsForm'] = $form->createView();

        $form->handleRequest($request);

        if ($form->isValid()) {

            $template   = $request->request->get('form');
            $recipients = $em->getRepository('IceMarktMainBundle:MailRecipient')->findAll();

            foreach ($recipients as $recipient) {
                $sendFacade->sendTemplateToRecipient($template['email_template_id'], $recipient);
            }
        }

        return $this->varsForTwig;
    }

    /**
     * Method for sending out emails
     *
     * @Route("/marketing/sendBatch/{templateId}/{offset}", name="send_batch");
     *
     * @param Request $request
     * @param Integer $templateId
     * @param Integer $offset
     *
     * @return Response (json)
     */
    public function sendEmailBatchAction(Request $request, $templateId, $offset)
    {
        $json['emailStatus'] = array(
            'sent' => array(),
            'failed' => array()
        );
        $em = $this->getDoctrine()->getManager();

        $sendFacade = new SendFacade(
            $em,
            $this->get('mailer')
        );

        $recipients = $em->getRepository('IceMarktMainBundle:MailRecipient')->findAll(null, 10, $offset);

        $json['recipients'] = count($recipients);

        foreach ($recipients as $recipient) {
            try {
                $sendFacade->sendTemplateToRecipient($templateId, $recipient);
                $json['emailStatus']['sent'][] = $recipient->getEmailAddress();
            } catch (Exception $e) {
                $json['emailStatus']['failed'][] = $recipient->getEmailAddress();
            }
        }

        $response = new Response(json_encode($json));
        $response->headers->set('Content-Type', 'application/json');

        return $response;
    }
}
{'fraction_non_alphanumeric': 0.1451978385915984, 'fraction_numerical': 0.004531985358201151, 'mean_word_length': 2.2751141552511416, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, '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': '3109187', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1576, 'n_words': 276}