WebHookBaseTrait::postMessage()   C
last analyzed

Complexity

Conditions 12
Paths 4

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 48
ccs 31
cts 31
cp 1
rs 5.1266
cc 12
eloc 28
nc 4
nop 2
crap 12

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 Distilleries\Messenger\Http\Controllers\Base;
4
5
/**
6
 * Created by PhpStorm.
7
 * User: mfrancois
8
 * Date: 12/08/2016
9
 * Time: 15:10
10
 */
11
12
use Distilleries\Messenger\Contracts\MessengerReceiverContract;
13
use \Log;
14
use Illuminate\Http\Request;
15
16
trait WebHookBaseTrait
17
{
18
19 16
    public function getValidHook(Request $request)
20
    {
21 16
        $hub          = $request->input('hub_mode');
22 16
        $verify_token = $request->input('hub_verify_token');
23
24 16
        if ($hub === 'subscribe' && urldecode($verify_token) === config('messenger.validation_token')) {
25 4
            return response($request->get('hub_challenge'));
26
        } else {
27 12
            return abort(403);
28
        }
29
    }
30
31
32 36
    public function postMessage(Request $request, MessengerReceiverContract $messenger)
33
    {
34 36
        $object = $request->input('object');
35 36
        $result = "";
36
37 36
        if (empty($object)) {
38 4
            return abort(403);
39
        }
40
41 32
        if ($object == 'page') {
42 32
            $entry = $request->input('entry');
43 32
            $entry = json_decode(json_encode($entry), false);
44
45 32
            if (empty($entry) || !is_array($entry)) {
46 8
                return abort(403);
47
            }
48
49 24
            foreach ($entry as $pageEntry) {
50
51 24
                if (!is_array($pageEntry->messaging)) {
52 4
                    continue;
53
                }
54
55 20
                foreach ($pageEntry->messaging as $messagingEvent) {
56
57 20
                    if (!empty($messagingEvent->optin)) {
58 4
                        $result .= $messenger->receivedAuthentication($messagingEvent);
59 3
                    } else {
60 16
                        if (!empty($messagingEvent->message)) {
61 4
                            $result .= $messenger->receivedMessage($messagingEvent);
62 3
                        } else {
63 12
                            if (!empty($messagingEvent->delivery)) {
64 4
                                $result .= $messenger->receivedDeliveryConfirmation($messagingEvent);
65 3
                            } else {
66 8
                                if (!empty($messagingEvent->postback)) {
67 4
                                    $result .= $messenger->receivedPostback($messagingEvent);
68 3
                                } else {
69 9
                                    $result .= $messenger->defaultHookUndefinedAction($messagingEvent);
70
                                }
71
                            }
72
                        }
73
                    }
74 15
                }
75 18
            }
76 18
        }
77
78 24
        return response($result);
79
    }
80
81
}