<commit_before><?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use AppBundle\Mailer\Utils\MessageUtils;
use Swift_Mailer;
use Swift_Message;

//TODO
//http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html

/**
 * @Route("/email")
 */
class EmailController extends RestController
{
    /**
     * @Route("/send/{transport}",defaults={ "transport" = "sendgrid"})
     * @Method({"POST"})
     * "", ""
     * 
      curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"toEmail":"<EMAIL>", "toName":"Elvis", "fromEmail":"<EMAIL>", "fromName":"Digital deputyship service", "subject": "subject!", "bodyText":"PLAIN BODY", "bodyHtml":"HTML <b>BODY</b>"}'  http://digideps-api.local/email/send
     */
    public function sendEmail($transport)
    {
        $data = $this->deserializeBodyContent();

        array_map(function($k) use ($data) {
            if (!array_key_exists($k, $data)) {
                throw new \InvalidArgumentException("Missing parameter $k");
            }
        }, ['toEmail', 'toName', 'fromEmail', 'fromName', 'subject', 'bodyText', 'bodyHtml']);

        $mailTransport = 'mailer.transport.sendgrid';
        if ($transport == 'secure-smtp') {
            $mailTransport = 'mailer.transport.secureSmtp';
        }

        $mailerService = new Swift_Mailer($this->container->get($mailTransport));
        $message = $mailerService->createMessage(); /* @var $message \Swift_Message */
        $message->setTo($data['toEmail'], $data['toName']);
        $message->setFrom($data['fromEmail'], $data['fromName']);

        $message->setSubject($data['subject']);
        $message->setBody($data['bodyText']);
        $message->addPart($data['bodyHtml'], 'text/html');

        if (!empty($data['attachments'])) {
            foreach ($data['attachments'] as $attachment) {
                $message->attach(new \Swift_Attachment($attachment['content'], $attachment['filename'], $attachment['contentType']));
            }
        }

        // behat-@ emails goes to file instead of real sending
        if ($this->isEmailToMock($message)) {
            $result = $this->writeMessageIntoFile($message);
        } else {
            $result = $mailerService->send($message);
        }

        return array('result' => $result);
    }

    /**
     * @param \Swift_Mime_Message $message
     * @return boolean
     */
    private function isEmailToMock(Swift_Message $message)
    {
        $addressToMockRegexp = $this->container->getParameter('email_mock_address');
        $path = $this->container->getParameter('email_mock_path');
        if (!$addressToMockRegexp || !$path) {<commit_msg>email_mock_ param can now be absent<commit_after><?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use AppBundle\Mailer\Utils\MessageUtils;
use Swift_Mailer;
use Swift_Message;

//TODO
//http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html

/**
 * @Route("/email")
 */
class EmailController extends RestController
{
    /**
     * @Route("/send/{transport}",defaults={ "transport" = "sendgrid"})
     * @Method({"POST"})
     * "", ""
     * 
      curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"toEmail":"<EMAIL>", "toName":"Elvis", "fromEmail":"<EMAIL>", "fromName":"Digital deputyship service", "subject": "subject!", "bodyText":"PLAIN BODY", "bodyHtml":"HTML <b>BODY</b>"}'  http://digideps-api.local/email/send
     */
    public function sendEmail($transport)
    {
        $data = $this->deserializeBodyContent();

        array_map(function($k) use ($data) {
            if (!array_key_exists($k, $data)) {
                throw new \InvalidArgumentException("Missing parameter $k");
            }
        }, ['toEmail', 'toName', 'fromEmail', 'fromName', 'subject', 'bodyText', 'bodyHtml']);

        $mailTransport = 'mailer.transport.sendgrid';
        if ($transport == 'secure-smtp') {
            $mailTransport = 'mailer.transport.secureSmtp';
        }

        $mailerService = new Swift_Mailer($this->container->get($mailTransport));
        $message = $mailerService->createMessage(); /* @var $message \Swift_Message */
        $message->setTo($data['toEmail'], $data['toName']);
        $message->setFrom($data['fromEmail'], $data['fromName']);

        $message->setSubject($data['subject']);
        $message->setBody($data['bodyText']);
        $message->addPart($data['bodyHtml'], 'text/html');

        if (!empty($data['attachments'])) {
            foreach ($data['attachments'] as $attachment) {
                $message->attach(new \Swift_Attachment($attachment['content'], $attachment['filename'], $attachment['contentType']));
            }
        }

        // behat-@ emails goes to file instead of real sending
        if ($this->isEmailToMock($message)) {
            $result = $this->writeMessageIntoFile($message);
        } else {
            $result = $mailerService->send($message);
        }

        return array('result' => $result);
    }

    /**
     * @param \Swift_Mime_Message $message
     * @return boolean
     */
    private function isEmailToMock(Swift_Message $message)
    {
        $addressToMockRegexp = $this->container->hasParameter('email_mock_address') ? $this->container->getParameter('email_mock_address') : null;
        $path = $this->container->hasParameter('email_mock_path') ? $this->container->getParameter('email_mock_path') : null;
        if (!$addressToMockRegexp || !$path) {
{'fraction_non_alphanumeric': 0.17476066144473454, 'fraction_numerical': 0.0, 'mean_word_length': 3.294469357249626, 'pattern_counts': {'":': 14, '<': 13, '<?xml version=': 0, '>': 51, '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': '5619145', 'n_tokens_mistral': 1695, 'n_tokens_neox': 1654, 'n_words': 287}