<commit_before><?php

namespace app\email\services;

use infuse\Utility as U;
use infuse\View;
use App;

if (!defined('EMAIL_QUEUE_NAME')) {
    define('EMAIL_QUEUE_NAME', 'emails');
}

class EmailService
{
    private $app;
    private $mandrill;
    private $smtp;
    private $nop;
    private $fromEmail;
    private $fromName;

    public function __construct(array $settings, App $app)
    {
        $this->app = $app;

        $this->fromEmail = U::array_value($settings, 'from_email');
        $this->fromName = U::array_value($settings, 'from_name');

        if ($settings['type'] == 'smtp') {
            $transport = \Swift_SmtpTransport::newInstance($settings['host'], $settings['port'])
              ->setUsername($settings['username'])
              ->setPassword($settings['password']);

            $this->smtp = \Swift_Mailer::newInstance($transport);
        } elseif ($settings['type'] == 'mandrill') {
            $this->mandrill = new \Mandrill($settings['key']);
        } elseif ($settings['type'] == 'nop') {
            $this->nop = true;
        }
    }

    /**
     * Queues an email.
     *
     * @param string $template name of template
     * @param array  $message
     *
     * @return boolean
     */
    public function queueEmail($template, array $message)
    {
        $message = $this->compressMessage($message);

        return $this->app['queue']->enqueue(
            EMAIL_QUEUE_NAME,
            [
                't' => $template,
                'm' => $message, ],
            [
                'timeout' => 60,
                'expires_in' => 2592000, ]);
    }

    /**
     * Sends an email.
     *
     * @param string $template name of template
     * @param array  $message
     *
     * @return array
     */
    public function sendEmail($template, array $message)
    {
        // render the body from the template
        if ($template) {
            if (!isset($message['html'])) {
                $htmlView = new View('emails/'.$template, $message);
                $message['html'] = $htmlView->render();
            }

            if (!isset($message['text'])) {
                $textView = new View('emails/text/'.$template, $message);
                $message['text'] = $textView->render();
            }
        }

        // figure out who email will be from
        if (!isset($message['from_email'])) {
            $message['from_email'] = $this->fromEmail;
        }

        if (!isset($message['from_name'])) {
            $message['from_name'] = $this->fromName;
        }

        // figure out recipients
        $to = [];
        $bcc = [];
        foreach ((array) $message['to'] as $item) {
            $type = U::array_value($item, 'type');
            if ($type == 'bcc') {
                $bcc[$item['email']] = $item['name'];
            } else {
                $to[$item['email']] = $item['name'];
            }
        }

        try {
            /* Mandrill API */
            if ($this->mandrill) {
                return $this->mandrill->messages->send($message);
            /* Swift Mailer SMTP */
            } elseif ($this->smtp) {
                $sMessage = \Swift_Message::newInstance($message['subject'])
                  ->setFrom([ $message['from_email'] => $message['from_name']])
                  ->setTo($to)
                  ->setBcc($bcc)
                  ->setBody($message['html'], 'text/html');

                if (isset($message['text'])) {
                    $sMessage->addPart($message['text'], 'text/plain');
                }

                if (isset($message['headers']) && is_array($message['headers'])) {
                    $headers = $sMessage->getHeaders();

                    foreach ($message['headers'] as $k => $v) {
                        $headers->addTextHeader($k, $v);
                    }
                }

                $sent = $this->smtp->send($sMessage);

                return array_fill(0, count($to), ['status' => ($sent) ? 'sent' : 'rejected']);
            /* NOP */
            } elseif ($this->nop) {
                $result = [];
                foreach (array_merge($to, $bcc) as $email => $name) {
                    $result[] = array_replace($message, [
                        'email' => $email,
                        '_id' => U::guid(false),<commit_msg>rename vendor namespace from `infuse` -> `Infuse`<commit_after><?php

namespace app\email\services;

use Infuse\Utility as U;
use Infuse\View;
use App;

if (!defined('EMAIL_QUEUE_NAME')) {
    define('EMAIL_QUEUE_NAME', 'emails');
}

class EmailService
{
    private $app;
    private $mandrill;
    private $smtp;
    private $nop;
    private $fromEmail;
    private $fromName;

    public function __construct(array $settings, App $app)
    {
        $this->app = $app;

        $this->fromEmail = U::array_value($settings, 'from_email');
        $this->fromName = U::array_value($settings, 'from_name');

        if ($settings['type'] == 'smtp') {
            $transport = \Swift_SmtpTransport::newInstance($settings['host'], $settings['port'])
              ->setUsername($settings['username'])
              ->setPassword($settings['password']);

            $this->smtp = \Swift_Mailer::newInstance($transport);
        } elseif ($settings['type'] == 'mandrill') {
            $this->mandrill = new \Mandrill($settings['key']);
        } elseif ($settings['type'] == 'nop') {
            $this->nop = true;
        }
    }

    /**
     * Queues an email.
     *
     * @param string $template name of template
     * @param array  $message
     *
     * @return bool
     */
    public function queueEmail($template, array $message)
    {
        $message = $this->compressMessage($message);

        return $this->app['queue']->enqueue(
            EMAIL_QUEUE_NAME,
            [
                't' => $template,
                'm' => $message, ],
            [
                'timeout' => 60,
                'expires_in' => 2592000, ]);
    }

    /**
     * Sends an email.
     *
     * @param string $template name of template
     * @param array  $message
     *
     * @return array
     */
    public function sendEmail($template, array $message)
    {
        // render the body from the template
        if ($template) {
            if (!isset($message['html'])) {
                $htmlView = new View('emails/'.$template, $message);
                $message['html'] = $htmlView->render();
            }

            if (!isset($message['text'])) {
                $textView = new View('emails/text/'.$template, $message);
                $message['text'] = $textView->render();
            }
        }

        // figure out who email will be from
        if (!isset($message['from_email'])) {
            $message['from_email'] = $this->fromEmail;
        }

        if (!isset($message['from_name'])) {
            $message['from_name'] = $this->fromName;
        }

        // figure out recipients
        $to = [];
        $bcc = [];
        foreach ((array) $message['to'] as $item) {
            $type = U::array_value($item, 'type');
            if ($type == 'bcc') {
                $bcc[$item['email']] = $item['name'];
            } else {
                $to[$item['email']] = $item['name'];
            }
        }

        try {
            /* Mandrill API */
            if ($this->mandrill) {
                return $this->mandrill->messages->send($message);
            /* Swift Mailer SMTP */
            } elseif ($this->smtp) {
                $sMessage = \Swift_Message::newInstance($message['subject'])
                  ->setFrom([$message['from_email'] => $message['from_name']])
                  ->setTo($to)
                  ->setBcc($bcc)
                  ->setBody($message['html'], 'text/html');

                if (isset($message['text'])) {
                    $sMessage->addPart($message['text'], 'text/plain');
                }

                if (isset($message['headers']) && is_array($message['headers'])) {
                    $headers = $sMessage->getHeaders();

                    foreach ($message['headers'] as $k => $v) {
                        $headers->addTextHeader($k, $v);
                    }
                }

                $sent = $this->smtp->send($sMessage);

                return array_fill(0, count($to), ['status' => ($sent) ? 'sent' : 'rejected']);
            /* NOP */
            } elseif ($this->nop) {
                $result = [];
                foreach (array_merge($to, $bcc) as $email => $name) {
                    $result[] = array_replace($message, [
                        'email' => $email,
                        '_id' => U::guid(false),
{'fraction_non_alphanumeric': 0.169820027688048, 'fraction_numerical': 0.0023073373327180432, 'mean_word_length': 1.6551301684532924, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 84, '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': '2204225', 'n_tokens_mistral': 2539, 'n_tokens_neox': 2399, 'n_words': 477}