MailGunController::handleWebhook()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 12
cts 12
cp 1
rs 9.2248
c 0
b 0
f 0
cc 5
nc 12
nop 1
crap 5
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 5
    public function __construct()
12
    {
13 5
        $this->middleware(MailgunWebhook::class);
14 5
    }
15
16 3
    public function handleWebhook(Request $request)
17
    {
18 3
        $data = $request->get('event-data');
19
20 3
        if (! isset($data['message']['headers']['message-id'])) {
21
            // If Mailgun receives a 406 (Not Acceptable) code, Mailgun will determine the POST is rejected and not retry.
22 1
            abort(406);
23
        }
24
25 2
        $message_id = $data['message']['headers']['message-id'];
26
27 2
        $delivery = EmailLog::where('provider', 'mailgun')->where('provider_email_id', $message_id)->first();
28
29 2
        if (! $delivery) {
30
            // If Mailgun receives a 406 (Not Acceptable) code, Mailgun will determine the POST is rejected and not retry.
31 1
            abort(406);
32
        }
33
34 1
        if (in_array($data['event'], ['opened', 'clicked', 'delivered', 'failed'])) {
35 1
            if ($delivery->{$data['event'].'_at'} == null) {
36 1
                $delivery->update(["{$data['event']}_at" => now()]);
37
            }
38
        }
39 1
    }
40
}
41