Test Failed
Push — master ( 1068e7...19b6ab )
by Thomas
02:42
created

MailGunController::handleWebhook()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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