GmailMessageService::getHtml()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 8
nc 6
nop 1
dl 0
loc 16
ccs 0
cts 9
cp 0
crap 42
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace App\Service;
4
5
use App\Util\StringUtil;
6
use DateTime;
7
use Google\Service\Gmail\Message;
8
use function Symfony\Component\String\u;
9
10
class GmailMessageService
11
{
12
    public function getCreatedAt(Message $message): DateTime
13
    {
14
        return new DateTime(sprintf('@%d', $message->internalDate / 1000));
15
    }
16
17
    public function getHeaders(Message $message): array
18
    {
19
        $headers = [];
20
21
        foreach ($message->getPayload()->getHeaders() as $header) {
22
            if ('From' === $header->name) {
23
                $headers['from'] = $header->value;
24
            }
25
26
            if ('Subject' === $header->name) {
27
                $headers['subject'] = $header->value;
28
            }
29
30
            if (2 === count($headers)) {
31
                break;
32
            }
33
        }
34
35
        return $headers;
36
    }
37
38
    public function getHtml(Message $message): string
39
    {
40
        $html = '';
41
        $payload = $message->getPayload();
42
43
        if ('text/html' === $payload->getMimeType() && null !== $body = $payload->getBody()->getData()) {
44
            $html .= StringUtil::base64UrlDecode($body);
45
        }
46
47
        foreach ($message->getPayload()->getParts() as $part) {
48
            if ('text/html' === $part->getMimeType() && null !== $body = $part->getBody()->getData()) {
49
                $html .= StringUtil::base64UrlDecode($body);
50
            }
51
        }
52
53
        return u($html)->collapseWhitespace();
54
    }
55
}
56