Completed
Push — master ( 792869...208c47 )
by Marcel
13s
created

InboundEmail::to()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace BeyondCode\Mailbox;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\Mail;
8
use EmailReplyParser\EmailReplyParser;
9
use Illuminate\Contracts\Mail\Mailable;
10
use Illuminate\Database\Eloquent\Model;
11
use ZBateson\MailMimeParser\Header\AddressHeader;
12
use ZBateson\MailMimeParser\Message as MimeMessage;
13
use ZBateson\MailMimeParser\Header\Part\AddressPart;
14
use ZBateson\MailMimeParser\Message\Part\MessagePart;
15
16
class InboundEmail extends Model
17
{
18
    protected $table = 'mailbox_inbound_emails';
19
20
    /** @var MimeMessage */
21
    protected $mimeMessage;
22
23
    protected $fillable = [
24
        'message',
25
    ];
26
27
    protected static function boot()
28
    {
29
        parent::boot();
30
31
        static::creating(function ($model) {
32
            $model->message_id = $model->id();
33
        });
34
    }
35
36
    public static function fromMessage($message)
37
    {
38
        return new static([
39
            'message' => $message,
40
        ]);
41
    }
42
43
    public function id(): string
44
    {
45
        return $this->message()->getHeaderValue('Message-Id', str_random());
46
    }
47
48
    public function date(): Carbon
49
    {
50
        return Carbon::make($this->message()->getHeaderValue('Date'));
51
    }
52
53
    public function text(): ?string
54
    {
55
        return $this->message()->getTextContent();
56
    }
57
58
    public function visibleText(): ?string
59
    {
60
        return EmailReplyParser::parseReply($this->text());
61
    }
62
63
    public function html(): ?string
64
    {
65
        return $this->message()->getHtmlContent();
66
    }
67
    
68
    public function headerValue($headerName): string
69
    {
70
        return $this->message()->getHeaderValue($headerName, null);
71
    }
72
73
    public function subject(): ?string
74
    {
75
        return $this->message()->getHeaderValue('Subject');
76
    }
77
78
    public function from(): string
79
    {
80
        $from = $this->message()->getHeader('From');
81
82
        if ($from instanceof AddressHeader) {
83
            return $from->getEmail();
84
        }
85
86
        return '';
87
    }
88
89
    public function fromName(): string
90
    {
91
        $from = $this->message()->getHeader('From');
92
93
        if ($from instanceof AddressHeader) {
94
            return $from->getPersonName();
95
        }
96
97
        return '';
98
    }
99
100
    /**
101
     * @return AddressPart[]
102
     */
103
    public function to(): array
104
    {
105
        return $this->convertAddressHeader($this->message()->getHeader('To'));
106
    }
107
108
    /**
109
     * @return AddressPart[]
110
     */
111
    public function cc(): array
112
    {
113
        return $this->convertAddressHeader($this->message()->getHeader('Cc'));
114
    }
115
116
    protected function convertAddressHeader($header): array
117
    {
118
        if ($header instanceof AddressHeader) {
119
            return Collection::make($header->getAddresses())->toArray();
120
        }
121
122
        return [];
123
    }
124
125
    /**
126
     * @return MessagePart[]
127
     */
128
    public function attachments()
129
    {
130
        return $this->message()->getAllAttachmentParts();
131
    }
132
133
    public function message(): MimeMessage
134
    {
135
        $this->mimeMessage = $this->mimeMessage ?: MimeMessage::from($this->message);
0 ignored issues
show
Bug introduced by
The property message does not seem to exist. Did you mean mimeMessage?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
136
137
        return $this->mimeMessage;
138
    }
139
140
    public function reply(Mailable $mailable)
141
    {
142
        if ($mailable instanceof \Illuminate\Mail\Mailable) {
143
            $mailable->withSwiftMessage(function (\Swift_Message $message) {
144
                $message->getHeaders()->addIdHeader('In-Reply-To', $this->id());
145
            });
146
        }
147
148
        return Mail::to($this->from())->send($mailable);
0 ignored issues
show
Compatibility introduced by
$mailable of type object<Illuminate\Contracts\Mail\Mailable> is not a sub-type of object<Illuminate\Mail\Mailable>. It seems like you assume a concrete implementation of the interface Illuminate\Contracts\Mail\Mailable to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
149
    }
150
151
    public function forward($recipients)
152
    {
153
        return Mail::send([], [], function ($message) use ($recipients) {
154
            $message->to($recipients)
155
                ->subject($this->subject())
156
                ->setBody($this->body(), $this->message()->getContentType());
157
        });
158
    }
159
160
    public function body()
161
    {
162
        return $this->isHtml() ? $this->html() : $this->text();
163
    }
164
165
    public function isHtml()
166
    {
167
        return empty($this->html());
168
    }
169
170
    public function isText()
171
    {
172
        return empty($this->text());
173
    }
174
175
    public function isValid(): bool
176
    {
177
        return $this->from() !== '' && ($this->text() !== '' || $this->html() !== '');
178
    }
179
}
180