Completed
Push — master ( dc7b66...2f29ec )
by Maxime
03:23
created

WebHookController::getValidHook()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 7
nc 2
nop 1
crap 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: mfrancois
5
 * Date: 31/07/2016
6
 * Time: 19:09
7
 */
8
9
namespace Distilleries\Messenger\Http\Controllers;
10
11
use Distilleries\Messenger\Contracts\MessengerReceiverContract;
12
use Illuminate\Routing\Controller;
13
use Log;
14
use Illuminate\Http\Request;
15
16
class WebHookController extends Controller
17
{
18 16
    public function getValidHook(Request $request)
19
    {
20 16
        $hub          = $request->input('hub_mode');
21 16
        $verify_token = $request->input('hub_verify_token');
22
23 16
        if ($hub === 'subscribe' && urldecode($verify_token) === config('messenger.validation_token')) {
24 4
            return response($request->get('hub_challenge'));
25
        } else {
26 12
            return abort(403);
27
        }
28
    }
29
30
31 36
    public function postMessage(Request $request, MessengerReceiverContract $messenger)
32
    {
33 36
        $object = $request->input('object');
34
35 36
        if (empty($object)) {
36 4
            return abort(403);
37
        }
38
39 32
        if ($object == 'page') {
40 32
            $entry = $request->input('entry');
41 32
            $entry = json_decode(json_encode($entry), false);
42
43 32
            if (empty($entry) || !is_array($entry)) {
44 8
                return abort(403);
45
            }
46
47 24
            $result = "";
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);
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...
79
    }
80
81
}