Passed
Pull Request — master (#7)
by
unknown
05:06
created

MailSentListener::handle()   C

Complexity

Conditions 12
Paths 61

Size

Total Lines 41
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 28
nc 61
nop 1
dl 0
loc 41
rs 6.9666
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Giuga\LaravelMailLog\Listeners;
4
5
use Giuga\LaravelMailLog\Models\MailLog;
6
use Giuga\LaravelMailLog\Traits\Occurrable;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Mail\Events\MessageSent;
9
use Illuminate\Support\Facades\Log;
10
11
class MailSentListener
12
{
13
    /**
14
     * Create the event listener.
15
     *
16
     * @return void
17
     */
18
    public function __construct()
19
    {
20
        //
21
    }
22
23
    /**
24
     * Handle the event.
25
     *
26
     * @param MessageSent $event
27
     * @return void
28
     */
29
    public function handle(MessageSent $event)
30
    {
31
        try {
32
            $msg = $event->message;
33
            $parts = $msg->getChildren();
34
            $body = $event->message->getBody();
35
            if (! empty($parts)) {
36
                foreach ($parts as $part) {
37
                    if (stripos($part->getBodyContentType(), 'image') !== false) {
38
                        $ptr = str_replace("\n", '', trim(str_replace($part->getHeaders(), '', $part->toString())));
39
                        $body = str_replace('cid:'.$part->getId(), 'data:'.$part->getBodyContentType().';base64,'.$ptr, $body);
40
                    }
41
                }
42
            }
43
44
            $to = $event->message->getTo() ?? [];
45
            $cc = $event->message->getCc() ?? [];
46
            $bcc = $event->message->getBcc() ?? [];
47
            $data = [
48
                'to' => implode(', ', is_array($to) ? array_keys($to) : $to),
0 ignored issues
show
introduced by
The condition is_array($to) is always true.
Loading history...
49
                'cc' => implode(', ', is_array($cc) ? array_keys($cc) : $cc),
0 ignored issues
show
introduced by
The condition is_array($cc) is always true.
Loading history...
50
                'bcc' => implode(', ', is_array($bcc) ? array_keys($bcc) : $bcc),
0 ignored issues
show
introduced by
The condition is_array($bcc) is always true.
Loading history...
51
                'subject' => $event->message->getSubject(),
52
                'message' => $body,
53
                'data' => [],
54
            ];
55
            $log = MailLog::create($data);
56
57
            $occuredEntity = $event->data[Occurrable::getOccuredEntityKey()] ?? null;
58
            $occuredProcess = $event->data[Occurrable::getOccuredProcessKey()] ?? null;
59
60
            if($occuredEntity && $occuredEntity instanceof Model) {
61
                $log->occurredEntity()->associate($occuredEntity)->save();
62
            }
63
64
            if($occuredProcess && $occuredProcess instanceof Model) {
65
                $log->occurredProcess()->associate($occuredProcess)->save();
66
            }
67
68
        } catch (\Throwable $e) {
69
            Log::debug('Failed to save mail log ['.$e->getMessage().']');
70
        }
71
    }
72
}
73