Completed
Push — master ( a3ab04...96da42 )
by Maxime
03:50
created

WebHookBaseTrait::postMessage()   C

Complexity

Conditions 12
Paths 4

Size

Total Lines 49
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 49
ccs 31
cts 31
cp 1
rs 5.1474
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 36
        if (empty($object)) {
37 4
            return abort(403);
38
        }
39
40 32
        if ($object == 'page') {
41 32
            $entry = $request->input('entry');
42 32
            $entry = json_decode(json_encode($entry), false);
43
44 32
            if (empty($entry) || !is_array($entry)) {
45 8
                return abort(403);
46
            }
47
48 24
            $result = "";
49
50 24
            foreach ($entry as $pageEntry) {
51
52 24
                if (!is_array($pageEntry->messaging)) {
53 4
                    continue;
54
                }
55
56 20
                foreach ($pageEntry->messaging as $messagingEvent) {
57
58 20
                    if (!empty($messagingEvent->optin)) {
59 4
                        $result .= $messenger->receivedAuthentication($messagingEvent);
60 3
                    } else {
61 16
                        if (!empty($messagingEvent->message)) {
62 4
                            $result .= $messenger->receivedMessage($messagingEvent);
63 3
                        } else {
64 12
                            if (!empty($messagingEvent->delivery)) {
65 4
                                $result .= $messenger->receivedDeliveryConfirmation($messagingEvent);
66 3
                            } else {
67 8
                                if (!empty($messagingEvent->postback)) {
68 4
                                    $result .= $messenger->receivedPostback($messagingEvent);
69 3
                                } else {
70 9
                                    $result .= $messenger->defaultHookUndefinedAction($messagingEvent);
71
                                }
72
                            }
73
                        }
74
                    }
75 15
                }
76 18
            }
77 18
        }
78
79 24
        return response($result);
0 ignored issues
show
Bug introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
80
    }
81
82
}