Passed
Pull Request — 4 (#10065)
by Steve
10:54
created

TestMailer::normaliseSpaces()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace SilverStripe\Dev;
4
5
use SilverStripe\Control\Email\Mailer;
6
use Swift_Attachment;
7
8
class TestMailer implements Mailer
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $emailsSent = [];
14
15
    public function send($email)
16
    {
17
        // Detect body type
18
        $htmlContent = null;
19
        $plainContent = null;
20
        if ($email->getSwiftMessage()->getContentType() === 'text/plain') {
21
            $type = 'plain';
22
            $plainContent = $email->getBody();
23
        } else {
24
            $type = 'html';
25
            $htmlContent = $email->getBody();
26
            $plainPart = $email->findPlainPart();
27
            if ($plainPart) {
28
                $plainContent = $plainPart->getBody();
29
            }
30
        }
31
32
        // Get attachments
33
        $attachedFiles = [];
34
        foreach ($email->getSwiftMessage()->getChildren() as $child) {
35
            if ($child instanceof Swift_Attachment) {
36
                $attachedFiles[] = [
37
                    'contents' => $child->getBody(),
38
                    'filename' => $child->getFilename(),
39
                    'mimetype' => $child->getContentType(),
40
                ];
41
            }
42
        }
43
44
        // Serialise email
45
        $serialised = [
46
            'Type' => $type,
47
            'To' => implode(';', array_keys($email->getTo() ?: [])),
48
            'From' => implode(';', array_keys($email->getFrom() ?: [])),
49
            'Subject' => $email->getSubject(),
50
            'Content' => $email->getBody(),
51
            'AttachedFiles' => $attachedFiles
52
        ];
53
        if ($plainContent) {
54
            $serialised['PlainContent'] = $plainContent;
55
        }
56
        if ($htmlContent) {
57
            $serialised['HtmlContent'] = $htmlContent;
58
        }
59
60
        $this->saveEmail($serialised);
61
62
        return true;
63
    }
64
65
    /**
66
     * Save a single email to the log
67
     *
68
     * @param array $data A map of information about the email
69
     */
70
    protected function saveEmail($data)
71
    {
72
        $this->emailsSent[] = $data;
73
    }
74
75
    /**
76
     * Clear the log of emails sent
77
     */
78
    public function clearEmails()
79
    {
80
        $this->emailsSent = [];
81
    }
82
83
    /**
84
     * Search for an email that was sent.
85
     * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression.
86
     *
87
     * @param string $to
88
     * @param string $from
89
     * @param string $subject
90
     * @param string $content
91
     * @return array|null Contains keys: 'Type', 'To', 'From', 'Subject', 'Content', 'PlainContent', 'AttachedFiles',
92
     *               'HtmlContent'
93
     */
94
    public function findEmail($to, $from = null, $subject = null, $content = null)
95
    {
96
        $compare = [
97
            'To' => $to,
98
            'From' => $from,
99
            'Subject' => $subject,
100
            'Content' => $content,
101
        ];
102
103
        foreach ($this->emailsSent as $email) {
104
            $matched = true;
105
106
            foreach (['To', 'From', 'Subject', 'Content'] as $field) {
107
                $emailValue = $email[$field];
108
                if ($value = $compare[$field]) {
109
                    if ($field == 'To') {
110
                        $emailValue = $this->normaliseSpaces($emailValue);
111
                        $value = $this->normaliseSpaces($value);
112
                    }
113
                    if ($value[0] == '/') {
114
                        $matched = preg_match($value, $emailValue);
115
                    } else {
116
                        $matched = ($value == $emailValue);
117
                    }
118
                    if (!$matched) {
119
                        break;
120
                    }
121
                }
122
            }
123
124
            if ($matched) {
125
                return $email;
126
            }
127
        }
128
        return null;
129
    }
130
131
    /**
132
     * @param string $value
133
     */
134
    private function normaliseSpaces(string $value)
135
    {
136
        return str_replace([', ', '; '], [',', ';'], $value);
137
    }
138
}
139