EmogrifierPlugin   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 71
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A sendPerformed() 0 2 1
A beforeSendPerformed() 0 20 6
A getEmogrifier() 0 3 1
A setEmogrifier() 0 4 1
A __construct() 0 6 2
1
<?php
2
3
namespace Bummzack\SwiftMailer\EmogrifyPlugin;
4
5
use Pelago\Emogrifier;
6
use Swift_Events_SendEvent;
7
use Swift_Events_SendListener;
8
9
/**
10
 * Emogrifier Plugin that will convert your CSS to inline styles.
11
 * To be used with SwiftMailer.
12
 *
13
 * @package Bummzack\SwiftMailer\EmogrifyPlugin
14
 */
15
class EmogrifierPlugin implements Swift_Events_SendListener
16
{
17
    /**
18
     * @var Emogrifier
19
     */
20
    protected $emogrifier;
21
22
    /**
23
     * @param Emogrifier $emogrifier
24
     */
25
    public function __construct(Emogrifier $emogrifier = null)
26
    {
27
        if ($emogrifier) {
28
            $this->emogrifier = $emogrifier;
29
        } else {
30
            $this->emogrifier = new Emogrifier();
0 ignored issues
show
Deprecated Code introduced by
The class Pelago\Emogrifier has been deprecated: Will be removed for version 4.0.0. Please use the CssInliner class instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

30
            $this->emogrifier = /** @scrutinizer ignore-deprecated */ new Emogrifier();
Loading history...
31
        }
32
    }
33
34
    /**
35
     * Access to the emogrifier instance that's being used internally
36
     * @return Emogrifier
37
     */
38
    public function getEmogrifier()
39
    {
40
        return $this->emogrifier;
41
    }
42
43
    /**
44
     * Set the emogrifier instance that's being used internally
45
     * @param Emogrifier $emogrifier
46
     * @return $this
47
     */
48
    public function setEmogrifier(Emogrifier $emogrifier)
49
    {
50
        $this->emogrifier = $emogrifier;
51
        return $this;
52
    }
53
54
    /**
55
     * @param Swift_Events_SendEvent $event
56
     */
57
    public function beforeSendPerformed(Swift_Events_SendEvent $event)
58
    {
59
        $message = $event->getMessage();
60
61
        $body = $message->getBody();
62
        if (!empty($body) && $message->getContentType() !== 'text/plain') {
63
            $this->emogrifier->setHtml($body);
64
            $message->setBody($this->emogrifier->emogrify());
65
        }
66
67
        foreach ($message->getChildren() as $messagePart) {
68
            if ($messagePart->getContentType() === 'text/html') {
69
                $body = $messagePart->getBody();
70
71
                if (empty($body)) {
72
                    continue;
73
                }
74
75
                $this->emogrifier->setHtml($body);
76
                $messagePart->setBody($this->emogrifier->emogrify());
77
            }
78
        }
79
    }
80
81
    /**
82
     * @param Swift_Events_SendEvent $event
83
     */
84
    public function sendPerformed(\Swift_Events_SendEvent $event)
85
    {
86
        /* No op */
87
    }
88
}
89