Completed
Pull Request — master (#64)
by Frederik
03:38 queued 01:09
created

FixedQuotation::quoteHtml()   C

Complexity

Conditions 10
Paths 15

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 10.0862

Importance

Changes 0
Metric Value
dl 0
loc 69
ccs 38
cts 42
cp 0.9048
rs 6.8096
c 0
b 0
f 0
cc 10
nc 15
nop 3
crap 10.0862

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Quotation;
5
6
use Genkgo\Mail\Address;
7
use Genkgo\Mail\AlternativeText;
8
use Genkgo\Mail\MessageBodyCollection;
9
use Genkgo\Mail\MessageInterface;
10
use Genkgo\Mail\QuotationInterface;
11
12
final class FixedQuotation implements QuotationInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    private $headerText;
18
19
    /**
20
     * @param string $headerText
21
     */
22 11
    public function __construct(string $headerText = '%s (%s):')
23
    {
24 11
        $this->headerText = $headerText;
25 11
    }
26
27
    /**
28
     * @param MessageBodyCollection $body
29
     * @param MessageInterface $originalMessage
30
     * @return MessageBodyCollection
31
     * @throws \DOMException
32
     */
33 11
    public function quote(MessageBodyCollection $body, MessageInterface $originalMessage): MessageBodyCollection
34
    {
35 11
        $originalBody = MessageBodyCollection::extract($originalMessage);
36 11
        $dateString = 'unknown';
37 11
        foreach ($originalMessage->getHeader('Date') as $header) {
38
            try {
39 4
                $date = new \DateTimeImmutable($header->getValue()->getRaw());
40 4
                $formatter = \IntlDateFormatter::create(
41 4
                    \Locale::getDefault(),
42 4
                    \IntlDateFormatter::MEDIUM,
43 4
                    \IntlDateFormatter::MEDIUM
44
                );
45
46 4
                if ($formatter === false) {
47
                    throw new \UnexpectedValueException('Cannot create date formatter');
48
                }
49
50 4
                $dateString = $formatter->format($date);
51 4
                break;
52
            } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
53
            }
54
        }
55
56 11
        $fromString = '';
57 11
        foreach ($originalMessage->getHeader('From') as $header) {
58 11
            $from = Address::fromString($header->getValue()->getRaw());
59 11
            $fromString = $from->getName() ? $from->getName() : (string)$from->getAddress();
60
        }
61
62 11
        $headerText = \sprintf($this->headerText, $fromString, $dateString);
63
64
        return $body
65 11
            ->withHtmlAndNoGeneratedAlternativeText(
66 11
                $this->quoteHtml(
67 11
                    $body->getHtml(),
68 11
                    $originalBody->getHtml(),
69
                    $headerText
70
                )
71
            )
72 11
            ->withAlternativeText(
73 11
                $this->quoteText(
74 11
                    $body->getText(),
75 11
                    $originalBody->getText(),
76
                    $headerText
77
                )
78
            );
79
    }
80
81
    /**
82
     * @param string $newHtml
83
     * @param string $originalHtml
84
     * @param string $headerText
85
     * @return string
86
     * @throws \DOMException
87
     */
88 11
    private function quoteHtml(string $newHtml, string $originalHtml, string $headerText): string
89
    {
90 11
        $originalHtml = \trim($originalHtml);
91 11
        if ($originalHtml === '') {
92 2
            return '';
93
        }
94
95 9
        $document = new \DOMDocument();
96 9
        $document->substituteEntities = false;
97 9
        $document->resolveExternals = false;
98
99 9
        $result = @$document->loadHTML($originalHtml);
100 9
        if ($result === false) {
101
            throw new \DOMException('Incorrect HTML');
102
        }
103
104 9
        if ($document->documentElement !== null) {
105 9
            $query = new \DOMXPath($document);
106 9
            $removeItems = $query->query('//head|//script|//body/@style|//html/@style', $document->documentElement);
107 9
            if ($removeItems instanceof \DOMNodeList) {
108
                /** @var \DOMElement $removeItem */
109 9
                foreach ($removeItems as $removeItem) {
110
                    /** @var \DOMElement $parent */
111 1
                    $parent = $removeItem->parentNode;
112 1
                    $parent->removeChild($removeItem);
113
                }
114
            }
115
116 9
            $body = $document->getElementsByTagName('body');
117 9
            $quote = $document->createElement('blockquote');
118 9
            $quote->setAttribute('type', 'cite');
119
120 9
            if ($body->length === 0) {
121
                $quote->appendChild($document->removeChild($document->documentElement));
122
            } else {
123 9
                $root = $body->item(0);
124 9
                if ($root instanceof \DOMElement) {
125 9
                    while ($root->childNodes->length !== 0) {
126
                        /** @var \DOMElement $child */
127 9
                        $child = $root->childNodes->item(0);
128 9
                        $quote->appendChild($child);
129
                    }
130
                }
131
            }
132
133 9
            $newDocument = new \DOMDocument();
134 9
            $newDocument->substituteEntities = false;
135 9
            $newDocument->resolveExternals = false;
136 9
            $result = @$newDocument->loadHTML($newHtml, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
137 9
            if ($result === false) {
138
                throw new \DOMException('Incorrect HTML');
139
            }
140
141 9
            $quotedNode = $newDocument->importNode($quote, true);
142 9
            $newBody = $this->prepareBody($newDocument);
143 9
            $newBody->appendChild($quotedNode);
144
145 9
            $header = $newDocument->createElement('p');
146 9
            $header->textContent = $headerText;
147
148
            /** @var \DOMElement $parent */
149 9
            $parent = $quotedNode->parentNode;
150 9
            $parent->insertBefore($header, $quotedNode);
151
152 9
            return \trim((string)$newDocument->saveHTML());
153
        }
154
155
        return '';
156
    }
157
158
    /**
159
     * @param \DOMDocument $document
160
     * @return \DOMElement
161
     */
162 9
    private function prepareBody(\DOMDocument $document): \DOMElement
163
    {
164 9
        if (!$document->documentElement) {
165
            throw new \UnexpectedValueException('Cannot prepare empty document');
166
        }
167
168 9
        $bodyList = $document->getElementsByTagName('body');
169 9
        if ($bodyList->length === 0) {
170
            $html = $document->createElement('html');
171
            $body = $document->createElement('body');
172
            $html->appendChild($body);
173
            $body->appendChild($document->documentElement);
174
            $document->removeChild($document->documentElement);
175
            $document->appendChild($html);
176
            return $body;
177
        }
178
179
        /** @var \DOMElement $body */
180 9
        $body = $bodyList->item(0);
181
182 9
        $queryHtml = new \DOMXPath($document);
183 9
        $htmlTags = $queryHtml->query('//html');
184 9
        if ($htmlTags && $htmlTags->length > 0) {
185
            /** @var \DOMElement $html */
186 9
            $html = $htmlTags->item(0);
187 9
            $html->appendChild($body);
188 9
            $document->removeChild($document->documentElement);
189 9
            $document->appendChild($html);
190 9
            return $body;
191
        }
192
193
        $html = $document->createElement('html');
194
        $html->appendChild($body);
195
        $document->removeChild($document->documentElement);
196
        $document->appendChild($html);
197
        return $body;
198
    }
199
200
    /**
201
     * @param AlternativeText $newText
202
     * @param AlternativeText $originalText
203
     * @param string $headerText
204
     * @return AlternativeText
205
     */
206 11
    private function quoteText(
207
        AlternativeText $newText,
208
        AlternativeText $originalText,
209
        string $headerText
210
    ): AlternativeText {
211 11
        return new AlternativeText(
212 11
            \sprintf(
213 11
                "%s\n\n%s\n>%s",
214 11
                (string)$newText,
215 11
                $headerText,
216 11
                \str_replace("\n", "\n>", $originalText->getRaw())
217
            )
218
        );
219
    }
220
}
221