Passed
Push — master ( 0716b8...6a0d02 )
by Jan
04:25
created

MailFromListener::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (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 General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
20
 */
21
22
namespace App\EventSubscriber;
23
24
25
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
26
use Symfony\Component\Mailer\Event\MessageEvent;
27
use Symfony\Component\Mime\Address;
28
use Symfony\Component\Mime\Email;
29
30
class MailFromListener implements EventSubscriberInterface
31
{
32
    protected $email;
33
    protected $name;
34
35
    public function __construct(string $email, string $name)
36
    {
37
        $this->email =  $email;
38
        $this->name = $name;
39
    }
40
41
    public function onMessage(MessageEvent $event): void
42
    {
43
        $address = new Address($this->email, $this->name);
44
        $event->getEnvelope()->setSender($address);
45
        $email = $event->getMessage();
46
        if ($email instanceof Email) {
47
            $email->from($address);
48
        }
49
    }
50
51
    public static function getSubscribedEvents()
52
    {
53
        return [
54
            // should be the last one to allow header changes by other listeners first
55
            MessageEvent::class => ['onMessage'],
56
        ];
57
    }
58
}