Message   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 90
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getParser() 0 9 2
A getDate() 0 6 1
A getBody() 0 11 3
A getAttachments() 0 6 1
A fixUniqueHeaders() 0 10 3
A fromFile() 0 6 1
1
<?php
2
/**
3
 * Webino (http://webino.sk/)
4
 *
5
 * @link        https://github.com/webino/WebinoDev/ for the canonical source repository
6
 * @copyright   Copyright (c) 2014-2018 Webino, s. r. o. (http://webino.sk/)
7
 * @license     BSD-3-Clause
8
 */
9
10
namespace WebinoDev\Mail;
11
12
use eXorus\PhpMimeMailParser\Parser;
13
use Zend\Mail\Headers;
14
use Zend\Mail\Message as BaseMessage;
15
16
/**
17
 * Extended Zend's mail message with attachments support
18
 */
19
class Message extends BaseMessage
20
{
21
    /**
22
     * @var Parser
23
     */
24
    private $parser;
25
26
    /**
27
     * Return mail parser
28
     *
29
     * @return Parser
30
     */
31
    private function getParser()
32
    {
33
        if (null === $this->parser) {
34
            $this->parser = new Parser;
35
            $this->fixUniqueHeaders();
36
            $this->parser->setText($this->toString());
37
        }
38
        return $this->parser;
39
    }
40
41
    /**
42
     * Return mail sent date
43
     *
44
     * @return string
45
     */
46
    public function getDate()
47
    {
48
        /** @var \Zend\Mail\Header\Date $date */
49
        $date = $this->getHeaders()->get('Date');
50
        return $date->getFieldValue();
51
    }
52
53
    /**
54
     * Return the message body
55
     *
56
     * @return string
57
     */
58
    public function getBody()
59
    {
60
        if ($this->getAttachments()) {
61
            try {
62
                return $this->getParser()->getMessageBody('html');
63
            } catch (\Throwable $exc) {
64
                error_log($exc);
65
            }
66
        }
67
        return parent::getBody();
0 ignored issues
show
Bug Compatibility introduced by
The expression parent::getBody(); of type string|object adds the type object to the return on line 67 which is incompatible with the return type documented by WebinoDev\Mail\Message::getBody of type string.
Loading history...
68
    }
69
70
    /**
71
     * Return array of mail attachments
72
     *
73
     * @return \eXorus\PhpMimeMailParser\Attachment[]
74
     */
75
    public function getAttachments()
76
    {
77
        /** @var \eXorus\PhpMimeMailParser\Attachment[] $attachments */
78
        $attachments = $this->getParser()->getAttachments();
79
        return $attachments;
80
    }
81
82
    /**
83
     * Fixing headers uniqueness
84
     */
85
    private function fixUniqueHeaders()
86
    {
87
        $headers = $this->getHeaders();
88
        $headersCopy = clone $headers;
89
        $headers->clearHeaders();
90
        foreach ($headersCopy as $header) {
91
            $name = $header->getFieldName();
92
            $headers->has($name) or $headers->addHeader($header);
93
        }
94
    }
95
96
    /**
97
     * Create mail object from file
98
     *
99
     * @param string $path Mail file path
100
     * @return self
101
     */
102
    public static function fromFile($path)
103
    {
104
        /** @var self $message */
105
        $message = static::fromString(join(Headers::EOL, file($path, FILE_IGNORE_NEW_LINES)));
106
        return $message;
107
    }
108
}
109