ImpersonatePlugin   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 41
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A beforeSendPerformed() 0 9 1
A sendPerformed() 0 9 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 10
    public function __construct($fromAddress)
30
    {
31 10
        $this->fromAddress = $fromAddress;
32 10
    }
33
34
    /**
35
     * @param \Swift_Events_SendEvent $event
36
     */
37 10
    public function beforeSendPerformed(\Swift_Events_SendEvent $event)
38
    {
39 10
        $message = $event->getMessage();
40 10
        $headers = $message->getHeaders();
41
42 10
        $headers->addMailboxHeader('X-Swift-From', $message->getFrom());
43
44 10
        $message->setFrom($this->fromAddress);
45 10
    }
46
47
    /**
48
     * @param \Swift_Events_SendEvent $event
49
     */
50 10
    public function sendPerformed(\Swift_Events_SendEvent $event)
51
    {
52 10
        $message = $event->getMessage();
53 10
        $headers = $message->getHeaders();
54
55 10
        $originalFrom = $headers->get('X-Swift-From')->getFieldBodyModel();
56
57 10
        $message->setFrom($originalFrom);
58 10
    }
59
}
60