Completed
Pull Request — master (#7)
by Barry vd.
06:50
created

InboundEmail::isValid()   A

Complexity

Conditions 3
Paths 3

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 3
nc 3
nop 0
1
<?php
2
3
namespace BeyondCode\Mailbox;
4
5
use Carbon\Carbon;
6
use EmailReplyParser\EmailReplyParser;
7
use Illuminate\Support\Collection;
8
use Illuminate\Support\Facades\Mail;
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 subject(): ?string
69
    {
70
        return $this->message()->getHeaderValue('Subject');
71
    }
72
73
    public function from(): string
74
    {
75
        $from = $this->message()->getHeader('From');
76
77
        if ($from instanceof AddressHeader) {
78
            return $from->getEmail();
79
        }
80
81
        return '';
82
    }
83
84
    public function fromName(): string
85
    {
86
        $from = $this->message()->getHeader('From');
87
88
        if ($from instanceof AddressHeader) {
89
            return $from->getPersonName();
90
        }
91
92
        return '';
93
    }
94
95
    /**
96
     * @return AddressPart[]
97
     */
98
    public function to(): array
99
    {
100
        return $this->convertAddressHeader($this->message()->getHeader('To'));
101
    }
102
103
    /**
104
     * @return AddressPart[]
105
     */
106
    public function cc(): array
107
    {
108
        return $this->convertAddressHeader($this->message()->getHeader('Cc'));
109
    }
110
111
    protected function convertAddressHeader($header): array
112
    {
113
        if ($header instanceof AddressHeader) {
114
            return Collection::make($header->getAddresses())->toArray();
115
        }
116
117
        return [];
118
    }
119
120
    /**
121
     * @return MessagePart[]
122
     */
123
    public function attachments()
124
    {
125
        return $this->message()->getAllAttachmentParts();
126
    }
127
128
    public function message(): MimeMessage
129
    {
130
        $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...
131
132
        return $this->mimeMessage;
133
    }
134
135
    public function reply(Mailable $mailable)
136
    {
137
        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...
138
    }
139
140
    public function forward($recipients)
141
    {
142
        return Mail::send([], [], function ($message) use ($recipients) {
143
            $message->to($recipients)
144
                ->subject($this->subject())
145
                ->setBody($this->body(), $this->message()->getContentType());
146
        });
147
    }
148
149
    public function body()
150
    {
151
        return $this->isHtml() ? $this->html() : $this->text();
152
    }
153
154
    public function isHtml()
155
    {
156
        return $this->html() !== '';
157
    }
158
159
    public function isText()
160
    {
161
        return $this->text() !== '';
162
    }
163
164
    public function isValid(): bool
165
    {
166
        return $this->from() !== '' && ($this->text() !== '' || $this->html() !== '');
167
    }
168
}
169