EmailLogEvent   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 48
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 26 4
A getFrom() 0 8 1
A getTo() 0 4 2
1
<?php
2
3
namespace Tompec\EmailLog;
4
5
use Illuminate\Mail\Events\MessageSending;
6
7
class EmailLogEvent
8
{
9
    /**
10
     * Handle the event.
11
     *
12
     * @param MessageSending $event
13
     */
14 3
    public function handle(MessageSending $event)
15
    {
16 3
        $message = $event->message;
17 3
        $headers = $message->getHeaders();
18
19 3
        $recipientModel = config('email-log.recipient_model');
20
21 3
        $recipientEmail = $this->getTo($headers);
22
23 3
        $recipient = $recipientModel::where(config('email-log.recipient_email_column'), $recipientEmail)->first();
24
25 3
        if ($recipient || config('email-log.log_unknown_recipients')) {
26 2
            EmailLog::create([
27 2
                'from' => $this->getFrom($headers),
28 2
                'to' => $recipientEmail,
29 2
                'subject' => $message->getSubject(),
30 2
                'body' => $message->getBody(),
31
32 2
                'provider' => config('mail.driver'),
33 2
                'provider_email_id' => $message->getId(),
34
35 2
                'recipient_type' => $recipient ? config('email-log.recipient_model') : null,
36 2
                'recipient_id' => optional($recipient)->id,
37
            ]);
38
        }
39 3
    }
40
41 2
    public function getFrom($headers)
42
    {
43 2
        return collect($headers->get('From')->getFieldBodyModel())
44
                    ->map(function ($name, $email) {
45 2
                        return "{$name} <{$email}>";
46 2
                    })
47 2
                    ->first();
48
    }
49
50 3
    public function getTo($headers)
51
    {
52 3
        return $headers->has('To') ? collect($headers->get('To')->getFieldBodyModel())->keys()->first() : null;
53
    }
54
}
55