<commit_before><?php
namespace AcMailer\Service;

use AcMailer\Event\MailEvent;
use AcMailer\Event\MailListener;
use AcMailer\Event\MailListenerAwareInterface;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\Mail\Transport\TransportInterface;
use Zend\Mail\Message;
use Zend\Mime\Message as MimeMessage;
use Zend\Mime\Part as MimePart;
use Zend\Mail\Transport\Exception\RuntimeException;
use AcMailer\Result\ResultInterface;
use AcMailer\Result\MailResult;
use Zend\Mime\Mime;
use Zend\View\Model\ViewModel;
use Zend\View\Renderer\RendererInterface;
use AcMailer\Exception\InvalidArgumentException;

/**
 * Wraps Zend\Mail functionality
 * @author <NAME>
 * @link http://www.alejandrocelaya.com
 */
class MailService implements MailServiceInterface, EventManagerAwareInterface, MailListenerAwareInterface
{
    
    /**
     * @var \Zend\Mail\Message
     */
    private $message;
    /**
     * @var \Zend\Mail\Transport\TransportInterface
     */
    private $transport;
    /**
     * @var \Zend\View\Renderer\PhpRenderer
     */
    private $renderer;
    /**
     * @var EventManagerInterface
     */
    private $events;
    /**
     * @var array
     */
    private $attachments = array();

    /**
     * Creates a new MailService
     * @param Message $message
     * @param TransportInterface $transport
     * @param RendererInterface $renderer Renderer used to render templates, typically a PhpRenderer
     */
    public function __construct(Message $message, TransportInterface $transport, RendererInterface $renderer)
    {
        $this->message      = $message;
        $this->transport    = $transport;
        $this->renderer     = $renderer;
    }
    
    /**
     * Returns this service's message
     * @return \Zend\Mail\Message
     * @see \AcMailer\Service\MailServiceInterface::getMessage()
     */
    public function getMessage()
    {
        return $this->message;
    }

    /**
     * Sends the mail
     * @return ResultInterface
     * @throws \Exception
     */
    public function send()
    {
        // Attach files before sending the email
        if (count($this->attachments) > 0) {
            $mimeMessage = $this->message->getBody();
            if (!$mimeMessage instanceof MimeMessage) {
                $this->setBody(new MimePart($mimeMessage));
                $mimeMessage = $this->message->getBody();
            }
            $bodyContent        = $mimeMessage->generateMessage();
            $bodyPart           = new MimePart($bodyContent);
            $bodyPart->type     = Mime::TYPE_HTML; // TODO
            $attachmentParts    = array();
            $info               = new \finfo(FILEINFO_MIME_TYPE);
            foreach ($this->attachments as $attachment) {
                if (!is_file($attachment)) {
                    continue; // If checked file is not valid, continue to the next
                }
                
                $part               = new MimePart(fopen($attachment, 'r'));
                $part->id	        = $attachment;
                $part->filename     = basename($attachment);
                $part->type         = $info->file($attachment);
                $part->encoding     = Mime::ENCODING_BASE64;
                $part->disposition  = Mime::DISPOSITION_ATTACHMENT;
                $attachmentParts[]  = $part;
            }
            array_unshift($attachmentParts, $bodyPart);
            $body = new MimeMessage();
            $body->setParts($attachmentParts);
            $this->message->setBody($body);
        }
        
        // Send the email
        try {
            // Trigger pre send event
            $this->getEventManager()->trigger(new MailEvent($this));

            // Try to send the message
            $this->transport->send($this->message);

            // Trigger post send event
            $this->getEventManager()->trigger(new MailEvent($this, MailEvent::EVENT_MAIL_POST_SEND));

            return new MailResult();
        } catch (RuntimeException $e) {
            // Trigger send error event
            $this->getEventManager()->trigger(new MailEvent($this, MailEvent::EVENT_MAIL_SEND_ERROR));

            return new MailResult(false, $e->getMessage());
        } catch (\Exception $e) {
            // Trigger send error event
            $this->getEventManager()->trigger(new MailEvent($this, MailEvent::EVENT_MAIL_SEND_ERROR));<commit_msg>Update MailService.php<commit_after><?php
namespace AcMailer\Service;

use AcMailer\Event\MailEvent;
use AcMailer\Event\MailListener;
use AcMailer\Event\MailListenerAwareInterface;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\Mail\Transport\TransportInterface;
use Zend\Mail\Message;
use Zend\Mime\Message as MimeMessage;
use Zend\Mime\Part as MimePart;
use Zend\Mail\Transport\Exception\RuntimeException;
use AcMailer\Result\ResultInterface;
use AcMailer\Result\MailResult;
use Zend\Mime\Mime;
use Zend\View\Model\ViewModel;
use Zend\View\Renderer\RendererInterface;
use AcMailer\Exception\InvalidArgumentException;

/**
 * Wraps Zend\Mail functionality
 * @author <NAME>
 * @link http://www.alejandrocelaya.com
 */
class MailService implements MailServiceInterface, EventManagerAwareInterface, MailListenerAwareInterface
{
    
    /**
     * @var \Zend\Mail\Message
     */
    private $message;
    /**
     * @var \Zend\Mail\Transport\TransportInterface
     */
    private $transport;
    /**
     * @var \Zend\View\Renderer\PhpRenderer
     */
    private $renderer;
    /**
     * @var EventManagerInterface
     */
    private $events;
    /**
     * @var array
     */
    private $attachments = array();

    /**
     * Creates a new MailService
     * @param Message $message
     * @param TransportInterface $transport
     * @param RendererInterface $renderer Renderer used to render templates, typically a PhpRenderer
     */
    public function __construct(Message $message, TransportInterface $transport, RendererInterface $renderer)
    {
        $this->message      = $message;
        $this->transport    = $transport;
        $this->renderer     = $renderer;
    }
    
    /**
     * Returns this service's message
     * @return \Zend\Mail\Message
     * @see \AcMailer\Service\MailServiceInterface::getMessage()
     */
    public function getMessage()
    {
        return $this->message;
    }

    /**
     * Sends the mail
     * @return ResultInterface
     * @throws \Exception
     */
    public function send()
    {
        // Attach files before sending the email
        if (count($this->attachments) > 0) {
            $mimeMessage = $this->message->getBody();
            if (!$mimeMessage instanceof MimeMessage) {
                $this->setBody(new MimePart($mimeMessage));
                $mimeMessage = $this->message->getBody();
            }
            $bodyContent        = $mimeMessage->generateMessage();
            $bodyPart           = new MimePart($bodyContent);
            $bodyPart->type     = Mime::TYPE_HTML; // TODO
            $attachmentParts    = array();
            $info               = new \finfo(FILEINFO_MIME_TYPE);
            foreach ($this->attachments as $attachment) {
                if (!is_file($attachment)) {
                    continue; // If checked file is not valid, continue to the next
                }
                
                $basename = basename($attachment);
                
                $part               = new MimePart(fopen($attachment, 'r'));
                $part->id           = $basename;
                $part->filename     = $basename;
                $part->type         = $info->file($attachment);
                $part->encoding     = Mime::ENCODING_BASE64;
                $part->disposition  = Mime::DISPOSITION_ATTACHMENT;
                $attachmentParts[]  = $part;
            }
            array_unshift($attachmentParts, $bodyPart);
            $body = new MimeMessage();
            $body->setParts($attachmentParts);
            $this->message->setBody($body);
        }
        
        // Send the email
        try {
            // Trigger pre send event
            $this->getEventManager()->trigger(new MailEvent($this));

            // Try to send the message
            $this->transport->send($this->message);

            // Trigger post send event
            $this->getEventManager()->trigger(new MailEvent($this, MailEvent::EVENT_MAIL_POST_SEND));

            return new MailResult();
        } catch (RuntimeException $e) {
            // Trigger send error event
            $this->getEventManager()->trigger(new MailEvent($this, MailEvent::EVENT_MAIL_SEND_ERROR));

            return new MailResult(false, $e->getMessage());
        } catch (\Exception $e) {
            // Trigger send error event
            $this->getEventManager()->trigger(new MailEvent($this, MailEvent::EVENT_MAIL_SEND_ERROR));
{'fraction_non_alphanumeric': 0.11619132171790035, 'fraction_numerical': 0.0006658528465209189, 'mean_word_length': 2.1587802313354363, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5920084', 'n_tokens_mistral': 2466, 'n_tokens_neox': 2404, 'n_words': 512}