Passed
Push — master ( e8f83f...f116c2 )
by Jan
05:37
created

SetMailFromSubscriber::onMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
declare(strict_types=1);
22
23
/**
24
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
25
 *
26
 * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
27
 *
28
 * This program is free software; you can redistribute it and/or
29
 * modify it under the terms of the GNU General Public License
30
 * as published by the Free Software Foundation; either version 2
31
 * of the License, or (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU General Public License
39
 * along with this program; if not, write to the Free Software
40
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
41
 */
42
43
namespace App\EventSubscriber;
44
45
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
46
use Symfony\Component\Mailer\Event\MessageEvent;
47
use Symfony\Component\Mime\Address;
48
use Symfony\Component\Mime\Email;
49
50
/**
51
 * This subscriber set the "From" field for all sent email, based on the global configured sender name and email.
52
 * @package App\EventSubscriber
53
 */
54
final class SetMailFromSubscriber implements EventSubscriberInterface
55
{
56
    private $email;
57
    private $name;
58
59
    public function __construct(string $email, string $name)
60
    {
61
        $this->email = $email;
62
        $this->name = $name;
63
    }
64
65
    public function onMessage(MessageEvent $event): void
66
    {
67
        $address = new Address($this->email, $this->name);
68
        $event->getEnvelope()->setSender($address);
69
        $email = $event->getMessage();
70
        if ($email instanceof Email) {
71
            $email->from($address);
72
        }
73
    }
74
75
    public static function getSubscribedEvents()
76
    {
77
        return [
78
            // should be the last one to allow header changes by other listeners first
79
            MessageEvent::class => ['onMessage'],
80
        ];
81
    }
82
}
83