Completed
Pull Request — master (#40)
by Bram
01:21
created

CssInlinerPlugin   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 50
ccs 13
cts 15
cp 0.8667
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A beforeSendPerformed() 0 14 4
A sendPerformed() 0 4 1
1
<?php
2
3
namespace Openbuildings\Swiftmailer;
4
5
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
6
use Swift_Events_SendListener;
7
use Swift_Events_SendEvent;
8
9
class CssInlinerPlugin implements Swift_Events_SendListener
10
{
11
    /**
12
     * @var CssToInlineStyles
13
     */
14
    private $converter;
15
16
    protected $contentTypes = [
17
        'text/html',
18
        'multipart/alternative'
19
    ];
20
    
21
    /**
22
     * @param CssToInlineStyles $converter
23
     */
24 1
    public function __construct(CssToInlineStyles $converter = null)
25
    {
26 1
        if ($converter) {
27 1
            $this->converter = $converter;
28
        } else {
29 1
            $this->converter = new CssToInlineStyles();
30
        }
31 1
    }
32
33
    /**
34
     * @param Swift_Events_SendEvent $event
35
     */
36 2
    public function beforeSendPerformed(Swift_Events_SendEvent $event)
37
    {
38 2
        $message = $event->getMessage();
39
40 2
        if (in_array($message->getContentType(), $this->contentTypes)) {
41 2
            $message->setBody($this->converter->convert($message->getBody()));
42
        }
43
44 2
        foreach ($message->getChildren() as $part) {
45
            if (in_array($part->getContentType(), $this->contentTypes)) {
46
                $part->setBody($this->converter->convert($part->getBody()));
47
            }
48
        }
49 2
    }
50
51
    /**
52
     * @param Swift_Events_SendEvent $event
53
     */
54 1
    public function sendPerformed(Swift_Events_SendEvent $event)
55
    {
56
        // Do nothing after sending the message
57 1
    }
58
}
59