<commit_before><?php

/*
 * This file is part of the ceo project.
 *
 * (c) <NAME> UCO <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace App\EventListener;

use App\Entity\Idea;
use App\Entity\Vote;
use App\Repository\IdeaRepository;
use FOS\CommentBundle\Event\CommentEvent;
use FOS\CommentBundle\Events;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Templating\EngineInterface;

class VotersNotifySubscriber implements EventSubscriberInterface
{
    /**
     * @var \Swift_Mailer
     */
    private $mailer;
    /**
     * @var EngineInterface
     */
    private $engine;
    /**
     * @var IdeaRepository
     */
    private $ideaRepository;
    /**
     * @var LoggerInterface
     */
    private $logger;
    /**
     * @var string
     */
    private $mailFrom;

    public function __construct(
        \Swift_Mailer $mailer,
        EngineInterface $engine,
        IdeaRepository $ideaRepository,
        LoggerInterface $logger,
        string $mailFrom
    ) {
        $this->mailer = $mailer;
        $this->engine = $engine;
        $this->ideaRepository = $ideaRepository;
        $this->logger = $logger;
        $this->mailFrom = $mailFrom;
    }

    public static function getSubscribedEvents()
    {
        return [
            Events::COMMENT_POST_PERSIST => 'commentWasDone',
        ];
    }

    public function commentWasDone(CommentEvent $event)
    {
        $comment = $event->getComment();
        if ($comment->getParent()) {
            $this->logger->debug('[MAIL IGNORE] Mensaje enviado como respuesta');

            return;
        }

        $ideaId = $comment->getThread()->getId();
        $idea = $this->ideaRepository->find($ideaId);
        if (!$idea instanceof Idea) {
            $this->logger->debug('[MAIL IGNORE] Idea no existe');

            return;
        }

        $ideaOwnerUsername = $idea->getOwner()->getUsername();
        $commenterUsername = $comment->getAuthorName();
        if ($ideaOwnerUsername !== $commenterUsername) {
            $this->logger->debug('[MAIL IGNORE] El que hace el comentario no es el dueño');

            return;
        }

        $toUsers = $idea->getVotes()->map(function (Vote $vote) {
            return $vote->getUser()->getUsername().'@uco.es';
        })->toArray();

        $this->logger->debug('[MAIL TO] Destinatarios: '.implode(', ', $toUsers));

        $message = (new \Swift_Message('[AulaSL] Comentario de organización'))
            ->setFrom($this->mailFrom)
            ->setTo($toUsers)
            ->setBody(
                $this->engine->render('mail/notify_new_author_comment.html.twig', [
                    'idea' => $idea,
                    'comment' => $comment,
                ]),
                'text/html'
            );

        $count = $this->mailer->send($message);

        $this->logger->debug('[MAIL TO] Recibidos por '.$count);
    }
}<commit_msg>Changed email receivers to Bcc<commit_after><?php

/*
 * This file is part of the ceo project.
 *
 * (c) <NAME> de la UCO <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace App\EventListener;

use App\Entity\Idea;
use App\Entity\Vote;
use App\Repository\IdeaRepository;
use FOS\CommentBundle\Event\CommentEvent;
use FOS\CommentBundle\Events;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Templating\EngineInterface;

class VotersNotifySubscriber implements EventSubscriberInterface
{
    /**
     * @var \Swift_Mailer
     */
    private $mailer;
    /**
     * @var EngineInterface
     */
    private $engine;
    /**
     * @var IdeaRepository
     */
    private $ideaRepository;
    /**
     * @var LoggerInterface
     */
    private $logger;
    /**
     * @var string
     */
    private $mailFrom;

    public function __construct(
        \Swift_Mailer $mailer,
        EngineInterface $engine,
        IdeaRepository $ideaRepository,
        LoggerInterface $logger,
        string $mailFrom
    ) {
        $this->mailer = $mailer;
        $this->engine = $engine;
        $this->ideaRepository = $ideaRepository;
        $this->logger = $logger;
        $this->mailFrom = $mailFrom;
    }

    public static function getSubscribedEvents()
    {
        return [
            Events::COMMENT_POST_PERSIST => 'commentWasDone',
        ];
    }

    public function commentWasDone(CommentEvent $event)
    {
        $comment = $event->getComment();
        if ($comment->getParent()) {
            $this->logger->debug('[MAIL IGNORE] Mensaje enviado como respuesta');

            return;
        }

        $ideaId = $comment->getThread()->getId();
        $idea = $this->ideaRepository->find($ideaId);
        if (!$idea instanceof Idea) {
            $this->logger->debug('[MAIL IGNORE] Idea no existe');

            return;
        }

        $ideaOwnerUsername = $idea->getOwner()->getUsername();
        $commenterUsername = $comment->getAuthorName();
        if ($ideaOwnerUsername !== $commenterUsername) {
            $this->logger->debug('[MAIL IGNORE] El que hace el comentario no es el dueño');

            return;
        }

        $toUsers = $idea->getVotes()->map(function (Vote $vote) {
            return $vote->getUser()->getUsername().'@uco.es';
        })->toArray();

        $this->logger->debug('[MAIL TO] Destinatarios: '.implode(', ', $toUsers));

        $message = (new \Swift_Message('[AulaSL] Comentario de organización'))
            ->setFrom($this->mailFrom)
            ->setTo($this->mailFrom)
            ->setBcc($toUsers)
            ->setBody(
                $this->engine->render('mail/notify_new_author_comment.html.twig', [
                    'idea' => $idea,
                    'comment' => $comment,
                ]),
                'text/html'
            );

        $count = $this->mailer->send($message);

        $this->logger->debug('[MAIL TO] Recibidos por '.$count);
    }
}
{'fraction_non_alphanumeric': 0.13757899854156538, 'fraction_numerical': 0.0, 'mean_word_length': 2.2348008385744236, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 91, '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': '4882660', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1780, 'n_words': 379}