Completed
Push — develop ( 5af0be...723ec8 )
by Peter
14:36
created

Message::fromFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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\Message as BaseMessage;
14
15
/**
16
 * Extended Zend's mail message with attachments support
17
 */
18
class Message extends BaseMessage
19
{
20
    /**
21
     * @var Parser
22
     */
23
    private $parser;
24
25
    /**
26
     * Return mail parser
27
     *
28
     * @return Parser
29
     */
30
    private function getParser()
31
    {
32
        if (null === $this->parser) {
33
            $this->parser = new Parser;
34
            $this->fixUniqueHeaders();
35
            $this->parser->setText($this->toString());
36
        }
37
        return $this->parser;
38
    }
39
40
    /**
41
     * Return mail sent date
42
     *
43
     * @return string
44
     */
45
    public function getDate()
46
    {
47
        /** @var \Zend\Mail\Header\Date $date */
48
        $date = $this->getHeaders()->get('Date');
49
        return $date->getFieldValue();
50
    }
51
52
    /**
53
     * Return the message body
54
     *
55
     * @return string
56
     */
57
    public function getBody()
58
    {
59
        if ($this->getAttachments()) {
60
            try {
61
                return $this->getParser()->getMessageBody('html');
62
            } catch (\Throwable $exc) {
63
                error_log($exc);
64
            }
65
        }
66
        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 66 which is incompatible with the return type documented by WebinoDev\Mail\Message::getBody of type string.
Loading history...
67
    }
68
69
    /**
70
     * Return array of mail attachments
71
     *
72
     * @return \eXorus\PhpMimeMailParser\Attachment[]
73
     */
74
    public function getAttachments()
75
    {
76
        /** @var \eXorus\PhpMimeMailParser\Attachment[] $attachments */
77
        $attachments = $this->getParser()->getAttachments();
78
        return $attachments;
79
    }
80
81
    /**
82
     * Fixing headers uniqueness
83
     */
84
    private function fixUniqueHeaders()
85
    {
86
        $headers = $this->getHeaders();
87
        $headersCopy = clone $headers;
88
        $headers->clearHeaders();
89
        foreach ($headersCopy as $header) {
90
            $name = $header->getFieldName();
91
            $headers->has($name) or $headers->addHeader($header);
92
        }
93
    }
94
95
    /**
96
     * Create mail object from file
97
     *
98
     * @param string $path Mail file path
99
     * @return self
100
     */
101
    public static function fromFile($path)
102
    {
103
        /** @var self $message */
104
        $message = static::fromString(join(PHP_EOL, file($path, FILE_IGNORE_NEW_LINES)));
105
        return $message;
106
    }
107
}
108