<commit_before><?php

namespace Questions\Service;

use Doctrine\ORM\EntityManager;
use Zend\Form\Form;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\Hydrator;
use Zend\Authentication\AuthenticationService;
use Questions\Entity\Question;
use Questions\Entity\Email;
use Zend\Mail;
use Zend\Mime\Part as MimePart;
use Zend\Mime\Message as MimeMessage;


class QuestionService
{
    public $_viewRenderer;
    /**
     * @var UserService
     */
    protected $_authenticationService;

    /**
     * @var ServiceManager
     */
    protected $_serviceManager;

    /**
     * @var Hydrator\ClassMethods
     */
    protected $_formHydrator;


    public function getQuestion($id)
    {
        $identity = $this->getAuthenticationService()->getIdentity();
        $objectManager = $this->getEntityManager()->getRepository('Questions\Entity\Question');

        $question = $objectManager->findOneBy(array(
            'user' => $identity->getId(),
            'id' => $id
        ));

        return $question;
    }

    public function getEmail($id, $valid = true)
    {
        $identity = $this->getAuthenticationService()->getIdentity();
        $objectManager = $this->getEntityManager()->getRepository('Questions\Entity\Email');

        $email = $objectManager->findOneBy(array(
            'id' => $id
        ));

        if (!$email || ($valid && $email->getQuestion()->getUser()->getId() != $identity->getId()))
            return null;

        return $email;
    }

    public function removeEmail(Email $email)
    {
        $entityManager = $this->getEntityManager();
        $entityManager->remove($email);
        $entityManager->flush();

        return $this;
    }

    public function removeQuestion(Question $question)
    {
        $entityManager = $this->getEntityManager();

        $emails = $question->getEmails();
        foreach ($emails as $email)
            $entityManager->remove($email);

        $answers = $question->getAnswers();
        foreach($answers as $answer)
            $entityManager->remove($answer);

        $entityManager->remove($question);
        $entityManager->flush();

        return $this;
    }

    public function getQuestions()
    {
        $identity = $this->getAuthenticationService()->getIdentity();
        return $identity->getQuestions();
    }

    public function sendQuestion(Question $question)
    {
        $emails = $question->getEmails();
        $transport = new Mail\Transport\Sendmail();
        foreach($emails as $email)
        {
            $view_renderer = $this->getViewRenderer();
            $content = $view_renderer->render('email/template/question',
                array(
                    'email' => $email,
                    'question' => $question
                ));

            // make a header as html
            $html = new MimePart($content);
            $html->type = "text/html";
            $body = new MimeMessage();
            $body->setParts(array($html,));

            $mail = new Mail\Message();<commit_msg>Don't send email if already sent<commit_after><?php

namespace Questions\Service;

use Doctrine\ORM\EntityManager;
use Zend\Form\Form;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\Hydrator;
use Zend\Authentication\AuthenticationService;
use Questions\Entity\Question;
use Questions\Entity\Email;
use Zend\Mail;
use Zend\Mime\Part as MimePart;
use Zend\Mime\Message as MimeMessage;


class QuestionService
{
    public $_viewRenderer;
    /**
     * @var UserService
     */
    protected $_authenticationService;

    /**
     * @var ServiceManager
     */
    protected $_serviceManager;

    /**
     * @var Hydrator\ClassMethods
     */
    protected $_formHydrator;


    public function getQuestion($id)
    {
        $identity = $this->getAuthenticationService()->getIdentity();
        $objectManager = $this->getEntityManager()->getRepository('Questions\Entity\Question');

        $question = $objectManager->findOneBy(array(
            'user' => $identity->getId(),
            'id' => $id
        ));

        return $question;
    }

    public function getEmail($id, $valid = true)
    {
        $identity = $this->getAuthenticationService()->getIdentity();
        $objectManager = $this->getEntityManager()->getRepository('Questions\Entity\Email');

        $email = $objectManager->findOneBy(array(
            'id' => $id
        ));

        if (!$email || ($valid && $email->getQuestion()->getUser()->getId() != $identity->getId()))
            return null;

        return $email;
    }

    public function removeEmail(Email $email)
    {
        $entityManager = $this->getEntityManager();
        $entityManager->remove($email);
        $entityManager->flush();

        return $this;
    }

    public function removeQuestion(Question $question)
    {
        $entityManager = $this->getEntityManager();

        $emails = $question->getEmails();
        foreach ($emails as $email)
            $entityManager->remove($email);

        $answers = $question->getAnswers();
        foreach($answers as $answer)
            $entityManager->remove($answer);

        $entityManager->remove($question);
        $entityManager->flush();

        return $this;
    }

    public function getQuestions()
    {
        $identity = $this->getAuthenticationService()->getIdentity();
        return $identity->getQuestions();
    }

    public function sendQuestion(Question $question)
    {
        $emails = $question->getEmails();
        $transport = new Mail\Transport\Sendmail();
        foreach($emails as $email)
        {
            if ($email->getState() !== Email::STATE_NEW)
                continue;

            $view_renderer = $this->getViewRenderer();
            $content = $view_renderer->render('email/template/question',
                array(
                    'email' => $email,
                    'question' => $question
                ));

            // make a header as html
            $html = new MimePart($content);
            $html->type = "text/html";
            $body = new MimeMessage();
            $body->setParts(array($html,));

            $mail = new Mail\Message();
{'fraction_non_alphanumeric': 0.1393615297358613, 'fraction_numerical': 0.0, 'mean_word_length': 2.3308148947652456, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 80, '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': '4552735', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1628, 'n_words': 324}