Conditions | 12 |
Paths | 61 |
Total Lines | 41 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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), |
||
|
|||
49 | 'cc' => implode(', ', is_array($cc) ? array_keys($cc) : $cc), |
||
50 | 'bcc' => implode(', ', is_array($bcc) ? array_keys($bcc) : $bcc), |
||
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 | } |
||
73 |