PlaintextMessage::generateTextBody()   D
last analyzed

Complexity

Conditions 9
Paths 10

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 35
ccs 0
cts 21
cp 0
rs 4.909
c 1
b 0
f 0
cc 9
eloc 20
nc 10
nop 1
crap 90
1
<?php
2
/**
3
 * MtMail - e-mail module for Zend Framework
4
 *
5
 * @link      http://github.com/mtymek/MtMail
6
 * @copyright Copyright (c) 2013-2017 Mateusz Tymek
7
 * @license   BSD 2-Clause
8
 */
9
10
namespace MtMail\ComposerPlugin;
11
12
use MtMail\Event\ComposerEvent;
13
use Zend\EventManager\AbstractListenerAggregate;
14
use Zend\EventManager\EventManagerInterface;
15
use Zend\Mime\Part as MimePart;
16
17
class PlaintextMessage extends AbstractListenerAggregate implements PluginInterface
18
{
19
20
    /**
21
     * (c) http://php.net/nl2br#86678
22
     *
23
     * @param  $string
24
     * @return mixed
25
     */
26
    private function br2nl($string)
27
    {
28
        return preg_replace('/\<br(\s*)?\/?\>/i', "\n", $string);
29
    }
30
31
    /**
32
     * Create text version of HTML message by changing line breaks to newlines and
33
     * stripping tags.
34
     *
35
     * @param ComposerEvent $event
36
     */
37
    public function generateTextBody(ComposerEvent $event)
38
    {
39
        $parts = $event->getBody()->getParts();
40
41
        $htmlBody = null;
42
        $htmlPart = null;
43
        $textBody = null;
44
45
        // locate HTML body
46
        foreach ($parts as $part) {
47
            foreach ($part->getHeadersArray() as $header) {
48
                if ($header[0] == 'Content-Type' && strpos($header[1], 'text/html') === 0) {
49
                    $htmlPart = $part;
50
                    $htmlBody = $part->getRawContent();
51
                } elseif ($header[0] == 'Content-Type' && strpos($header[1], 'text/plain') === 0) {
52
                    $textBody = $part->getRawContent();
53
                }
54
            }
55
        }
56
57
        if ($textBody || !$htmlBody) {
58
            // can only work if HTML body exists and text doesn't
59
            return;
60
        }
61
62
        // create and insert text body
63
        $textBody = strip_tags($this->br2nl($htmlBody));
64
        $textPart = new MimePart($textBody);
65
        $textPart->type = 'text/plain';
66
        $event->getBody()->setParts([$textPart, $htmlPart]);
67
68
        // force multipart/alternative content type
69
        $event->getMessage()->getHeaders()->get('content-type')->setType('multipart/alternative')
70
            ->addParameter('boundary', $event->getBody()->getMime()->boundary());
71
    }
72
73
    /**
74
     * Attach one or more listeners
75
     *
76
     * Implementors may add an optional $priority argument; the EventManager
77
     * implementation will pass this to the aggregate.
78
     *
79
     * @param EventManagerInterface $events
80
     *
81
     * @return void
82
     */
83
    public function attach(EventManagerInterface $events, $priority = 1)
84
    {
85
        $this->listeners[] = $events->attach(ComposerEvent::EVENT_COMPOSE_POST, [$this, 'generateTextBody']);
86
    }
87
}
88