Passed
Push — master ( 19b6ab...f5d191 )
by Thomas
06:22
created

MailGunController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 27
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handleWebhook() 0 19 4
1
<?php
2
3
namespace Tompec\EmailLog;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Routing\Controller;
7
use Tompec\EmailLog\Middlewares\MailgunWebhook;
8
9
class MailGunController extends Controller
10
{
11 4
    public function __construct()
12
    {
13 4
        $this->middleware(MailgunWebhook::class);
14 4
    }
15
16 2
    public function handleWebhook(Request $request)
17
    {
18 2
        $data = $request->get('event-data');
19
20 2
        $message_id = $data['message']['headers']['message-id'];
21
22 2
        $delivery = EmailLog::where('provider', 'mailgun')->where('provider_email_id', $message_id)->first();
23
24 2
        if (! $delivery) {
25
            // If Mailgun receives a 406 (Not Acceptable) code, Mailgun will determine the POST is rejected and not retry.
26 1
            abort(406);
27
        }
28
29 1
        if (in_array($data['event'], ['opened', 'clicked', 'delivered', 'failed'])) {
30 1
            if ($delivery->{$data['event'].'_at'} == null) {
31 1
                $delivery->update(["{$data['event']}_at" => now()]);
32
            }
33
        }
34 1
    }
35
}
36