WebhooksController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 4
dl 0
loc 32
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 20 2
A determineValidRequest() 0 8 2
1
<?php
2
3
namespace Appvise\AppStoreNotifications;
4
5
use Illuminate\Http\Request;
6
use Appvise\AppStoreNotifications\Model\NotificationType;
7
use Appvise\AppStoreNotifications\Model\AppleNotification;
8
use Appvise\AppStoreNotifications\Exceptions\WebhookFailed;
9
use Appvise\AppStoreNotifications\Model\NotificationPayload;
10
11
class WebhooksController
12
{
13
    public function __invoke(Request $request)
14
    {
15
        $jobConfigKey = NotificationType::{$request->input('notification_type')}();
16
        $this->determineValidRequest($request->input('password'));
17
18
        AppleNotification::storeNotification($jobConfigKey, $request->input());
19
20
        $payload = NotificationPayload::createFromRequest($request);
21
22
        $jobClass = config("appstore-server-notifications.jobs.{$jobConfigKey}", null);
23
24
        if (is_null($jobClass)) {
25
            throw WebhookFailed::jobClassDoesNotExist($jobConfigKey);
26
        }
27
28
        $job = new $jobClass($payload);
29
        dispatch($job);
30
31
        return response()->json();
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
32
    }
33
34
    private function determineValidRequest(string $password): bool
35
    {
36
        if ($password !== config('appstore-server-notifications.shared_secret')) {
37
            throw WebhookFailed::nonValidRequest();
38
        }
39
40
        return true;
41
    }
42
}
43