Commit fb9e09e9 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Nouvelle classe MailWriter : writer de log Laminas envoyant un mail avec le MailService.

parent 5cde8b4d
Loading
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
Journal des changements
=======================

7.5.0
-----
- Nouvelle classe MailWriter : writer de log Laminas envoyant un mail avec le MailService.

7.4.0 (02/12/2025)
------------------

- utilisation de la version 7.0 de unicaen/privilege
- Possibilité d'utiliser de la version 7.0 de unicaen/privilege

7.3.0
----
+1 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
        }
    ],
    "require": {
        "laminas/laminas-log": "*",
        "unicaen/privilege": "^6|^7",
        "symfony/mailer": "^7"
    },
+9 −0
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@

use UnicaenMail\Controller\MailController;
use UnicaenMail\Controller\MailControllerFactory;
use UnicaenMail\Log\Writer\MailWriter;
use UnicaenMail\Log\Writer\MailWriterFactory;
use UnicaenMail\Provider\Privilege\MailPrivileges;
use UnicaenMail\Service\Mail\MailService;
use UnicaenMail\Service\Mail\MailServiceFactory;
@@ -152,6 +154,13 @@ return array(
    'hydrators' => [
        'factories' => [],
    ],

    'log_writers' => [
        'factories' => [
            MailWriter::class => MailWriterFactory::class,
        ],
    ],

    'service_manager' => [
        'factories' => [
            MailService::class => MailServiceFactory::class,
+94 −0
Original line number Diff line number Diff line
<?php

namespace UnicaenMail\Log\Writer;

use Exception;
use InvalidArgumentException;
use Laminas\Log\Formatter\Simple;
use Laminas\Log\Writer\AbstractWriter;
use RuntimeException;
use UnicaenMail\Service\Mail\MailServiceAwareTrait;

class MailWriter extends AbstractWriter
{
    use MailServiceAwareTrait;

    protected ?string $to = null;
    protected ?string $subject = null;
    protected ?string $texte = null;
    protected ?string $module = null;
    protected array|string|null $attachement_path = null;
    protected array|string|null $copie = null;
    protected array|string|null $reply_to = null;
    protected bool $message_auto = true;

    public function __construct($options = null)
    {
        $this->processOptions($options);

        parent::__construct($options);

        $this->formatter = new Simple();
    }

    protected function processOptions(array &$options): void
    {
        $this->to = $options['to'] ?? $this->to;
        $this->subject = $options['subject'] ?? $this->subject;
        $this->texte = $options['texte'] ?? $this->texte;
        $this->module = $options['module'] ?? $this->module;
        $this->attachement_path = $options['attachement_path'] ?? $this->attachement_path;
        $this->copie = $options['copie'] ?? $this->copie;
        $this->reply_to = $options['reply_to'] ?? $this->reply_to;
        $this->message_auto = $options['message_auto'] ?? $this->message_auto;

        unset($options['to']);
        unset($options['subject']);
        unset($options['texte']);
        unset($options['module']);
        unset($options['attachement_path']);
        unset($options['copie']);
        unset($options['reply_to']);
        unset($options['message_auto']);
    }

    protected function doWrite(array $event): void
    {
        $to = $event['extra']['to'] ?? $this->to;
        $subject = $event['extra']['subject'] ?? $this->subject;
        $texte = $event['extra']['texte'] ?? $event['message'] ?? $this->texte;
        $module = $event['extra']['module'] ?? $this->module;
        $attachement_path = $event['extra']['attachement_path'] ?? $this->attachement_path;
        $copie = $event['extra']['copie'] ?? $this->copie;
        $reply_to = $event['extra']['reply_to'] ?? $this->reply_to;
        $message_auto = $event['extra']['message_auto'] ?? $this->message_auto;

        if (!$to) {
            throw new InvalidArgumentException("Aucun destinataire spécifié pour le mail de log à envoyer");
        }
        if (!$subject) {
            throw new InvalidArgumentException("Aucun sujet spécifié pour le mail de log à envoyer");
        }
        if (!$texte) {
            throw new InvalidArgumentException("Aucun texte spécifié pour le mail de log à envoyer");
        }

        $event['message'] = $texte;
        $texte = $this->formatter->format($event);

        try {
            $this->mailService->sendMail(
                $to,
                $subject,
                $texte,
                $module,
                $attachement_path,
                $copie,
                $reply_to,
                $message_auto,
            );
        } catch (Exception $e) {
            throw new RuntimeException("Une erreur est survenue lors de l'envoi du mail de log", previous: $e);
        }
    }
}
 No newline at end of file
+21 −0
Original line number Diff line number Diff line
<?php

namespace UnicaenMail\Log\Writer;

use Laminas\ServiceManager\Factory\FactoryInterface;
use Psr\Container\ContainerInterface;
use UnicaenMail\Service\Mail\MailService;

class MailWriterFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): MailWriter
    {
        $writer = new MailWriter($options);

        /** @var MailService $mailService */
        $mailService = $container->get(MailService::class);
        $writer->setMailService($mailService);

        return $writer;
    }
}
 No newline at end of file