Commit adb99384 authored by Jean-Philippe Metivier's avatar Jean-Philippe Metivier
Browse files

Merge branch 'feature_mail_table'

parents f4bc5c2d d988f34c
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <entity name="Notification\Entity\NotifMail" table="NOTIF_MAIL">
        <id name="id" type="integer" column="ID">
            <generator strategy="SEQUENCE"/>
        </id>

        <field name="subject" column="SUBJECT"/>
        <field name="to" column="MAIL_TO"/>
        <field name="from" column="MAIL_FROM"/>
        <field name="body" column="BODY_TEXT"/>
        <field name="sentOn" type="datetime" column="SENT_ON"/>

    </entity>
</doctrine-mapping>
+130 −0
Original line number Diff line number Diff line
<?php

namespace Notification\Entity;

use DateTime;

class NotifMail {

    /** @var integer */
    private $id;
    /** @var string|null */
    private $from;
    /** @var string|null */
    private $to;
    /** @var string|null */
    private $subject;
    /** @var string|null */
    private $body;
    /** @var DateTime|null */
    private $sentOn;

    /**
     * @return int
     */
    public function getId(): int
    {
        return $this->id;
    }

    /**
     * @param int $id
     * @return NotifMail
     */
    public function setId(int $id): NotifMail
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return string|null
     */
    public function getFrom(): ?string
    {
        return $this->from;
    }

    /**
     * @param string|null $from
     * @return NotifMail
     */
    public function setFrom(?string $from): NotifMail
    {
        $this->from = $from;
        return $this;
    }

    /**
     * @return string|null
     */
    public function getTo(): ?string
    {
        return $this->to;
    }

    /**
     * @param string|null $to
     * @return NotifMail
     */
    public function setTo(?string $to): NotifMail
    {
        $this->to = $to;
        return $this;
    }

    /**
     * @return string|null
     */
    public function getSubject(): ?string
    {
        return $this->subject;
    }

    /**
     * @param string|null $subject
     * @return NotifMail
     */
    public function setSubject(?string $subject): NotifMail
    {
        $this->subject = $subject;
        return $this;
    }

    /**
     * @return string|null
     */
    public function getBody(): ?string
    {
        return $this->body;
    }

    /**
     * @param string|null $body
     * @return NotifMail
     */
    public function setBody(?string $body): NotifMail
    {
        $this->body = $body;
        return $this;
    }

    /**
     * @return DateTime|null
     */
    public function getSentOn(): ?DateTime
    {
        return $this->sentOn;
    }

    /**
     * @param DateTime|null $sentOn
     * @return NotifMail
     */
    public function setSentOn(?DateTime $sentOn): NotifMail
    {
        $this->sentOn = $sentOn;
        return $this;
    }

}
+23 −4
Original line number Diff line number Diff line
@@ -2,9 +2,12 @@

namespace Notification\Service;

use DateTime;
use Notification\Entity\NotifMail;
use Notification\Entity\Service\NotifEntityServiceAwareTrait;
use Notification\MessageContainer;
use Notification\Notification;
use UnicaenApp\Service\EntityManagerAwareTrait;
use UnicaenApp\Service\Mailer\MailerServiceAwareTrait;
use Zend\Mail\Message;

@@ -17,6 +20,7 @@ class NotifierService
{
    use NotifEntityServiceAwareTrait;
    use MailerServiceAwareTrait;
    use EntityManagerAwareTrait;

    /**
     * @var MessageContainer
@@ -75,7 +79,6 @@ class NotifierService
    public function trigger(Notification $notification)
    {
        $notification->prepare();

        $this->sendNotification($notification);

        // collecte des éventuels messages exposés par la notification
@@ -92,12 +95,28 @@ class NotifierService
     */
    protected function sendNotification(Notification $notification)
    {
        $mail = $this->createMailForNotification($notification);

        $message = $this->mailerService->send($mail);
        $email = $this->createMailForNotification($notification);
        $nMail = new NotifMail();

        $mails = [];
        foreach ($email->getFrom() as $mail) { $mails[] = $mail->getEmail(); }
        $nMail->setFrom(implode(',', $mails));
        $mails = [];
        foreach ($email->getTo() as $mail) { $mails[] = $mail->getEmail(); }
        $nMail->setTo(implode(',', $mails));
        $nMail->setSubject($email->getSubject());
        $body = $email->getBodyText();
        //$body = htmlentities($body);
        $nMail->setBody($body);
        $nMail->setSentOn(new DateTime());
        $this->getEntityManager()->persist($nMail);
        $this->getEntityManager()->flush($nMail);

        $message = $this->mailerService->send($email);

        $sendDate = $this->extractDateFromMessage($message) ?: new \DateTime();
        $notification->setSendDate($sendDate);

    }

    /**
+1 −0
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ class NotifierServiceFactory
        $service->setNotifEntityService($notifEntityService);
        $service->setMailerService($mailerService);
        $service->setOptions($options);
        $service->setEntityManager($container->get('doctrine.entitymanager.orm_default'));

        return $service;
    }
+0 −18
Original line number Diff line number Diff line
@@ -621,25 +621,7 @@ class NotifierSoutenanceService extends NotifierService
                    'url' => $url,
                ]);
            $this->trigger($notif);
        }
    }


    /**
     * @param Notification $notification
     */
    public function trigger(Notification $notification)
    {
        $notification->prepare();

        $this->sendNotification($notification);

        // collecte des éventuels messages exposés par la notification
        foreach ($notification->getInfoMessages() as $message) {
            $this->messageContainer->setMessage($message, 'info');
        }
        foreach ($notification->getWarningMessages() as $message) {
            $this->messageContainer->setMessage($message, 'warning');
        }
    }