Total Complexity | 9 |
Total Lines | 48 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
16 | class EmogrifierPlugin implements Swift_Events_SendListener |
||
17 | { |
||
18 | protected $css = null; |
||
19 | |||
20 | public function getCss(): ?string |
||
21 | { |
||
22 | return $this->css; |
||
23 | } |
||
24 | |||
25 | public function setCss($value): self |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @param Swift_Events_SendEvent $evt |
||
33 | * @throws ParseException |
||
34 | */ |
||
35 | public function beforeSendPerformed(Swift_Events_SendEvent $evt) |
||
36 | { |
||
37 | $message = $evt->getMessage(); |
||
38 | |||
39 | $body = $message->getBody(); |
||
40 | if (!empty($body) && $message->getContentType() !== 'text/plain') { |
||
41 | $html = CssInliner::fromHtml($body)->inlineCss($this->css ?? '')->renderBodyContent(); |
||
42 | $message->setBody($html); |
||
43 | } |
||
44 | |||
45 | foreach ($message->getChildren() as $messagePart) { |
||
46 | if ($messagePart->getContentType() === 'text/html') { |
||
47 | $body = $messagePart->getBody(); |
||
48 | |||
49 | if (empty($body)) { |
||
50 | continue; |
||
51 | } |
||
52 | |||
53 | $html = CssInliner::fromHtml($body)->inlineCss($this->css ?? '')->renderBodyContent(); |
||
54 | $messagePart->setBody($html); |
||
55 | } |
||
56 | } |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param Swift_Events_SendEvent $evt |
||
61 | */ |
||
62 | public function sendPerformed(\Swift_Events_SendEvent $evt) |
||
64 | /* No op */ |
||
65 | } |
||
66 | } |
||
67 |