Completed
Push — master ( da405c...525739 )
by Emily
11s
created

ImpersonatePlugin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the pixelart Swiftmailer manipulator bundle.
5
 *
6
 * (c) pixelart GmbH
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Pixelart\Bundle\SwiftmailerManipulatorBundle\Swift\Plugins;
13
14
/**
15
 * Allows changing sender's address before sending the message.
16
 *
17
 * @author Dejan Angelov <[email protected]>
18
 */
19
class ImpersonatePlugin implements \Swift_Events_SendListener
20
{
21
    /**
22
     * @var string
23
     */
24
    private $fromAddress;
25
26
    /**
27
     * @param string $fromAddress Address the message should be sent from
28
     */
29
    public function __construct($fromAddress)
30
    {
31
        $this->fromAddress = $fromAddress;
32
    }
33
34
    /**
35
     * @param \Swift_Events_SendEvent $event
36
     */
37
    public function beforeSendPerformed(\Swift_Events_SendEvent $event)
38
    {
39
        $message = $event->getMessage();
40
        $headers = $message->getHeaders();
41
42
        $headers->addMailboxHeader('X-Swift-From', $message->getFrom());
43
44
        $message->setFrom($this->fromAddress);
45
    }
46
47
    /**
48
     * @param \Swift_Events_SendEvent $event
49
     */
50
    public function sendPerformed(\Swift_Events_SendEvent $event)
51
    {
52
        $message = $event->getMessage();
53
        $headers = $message->getHeaders();
54
55
        $originalFrom = $headers->get('X-Swift-From')->getFieldBodyModel();
56
57
        $message->setFrom($originalFrom);
58
    }
59
}
60