Completed
Pull Request — master (#44)
by Rias
05:47
created

StripeSignatureValidator::isValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
4
namespace Spatie\StripeWebhooks;
5
6
7
use Exception;
8
use Illuminate\Http\Request;
9
use Spatie\WebhookClient\SignatureValidator\SignatureValidator;
10
use Spatie\WebhookClient\WebhookConfig;
11
use Stripe\Webhook;
12
13
class StripeSignatureValidator implements SignatureValidator
14
{
15
    public function isValid(Request $request, WebhookConfig $config): bool
16
    {
17
        $signature = $request->header('Stripe-Signature');
18
        $secret = $config->signingSecret;
19
20
        try {
21
            Webhook::constructEvent($request->getContent(), $signature, $secret);
0 ignored issues
show
Bug introduced by
It seems like $request->getContent() targeting Symfony\Component\HttpFo...n\Request::getContent() can also be of type resource; however, Stripe\Webhook::constructEvent() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $signature defined by $request->header('Stripe-Signature') on line 17 can also be of type array or null; however, Stripe\Webhook::constructEvent() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
22
        } catch (Exception $exception) {
23
            return false;
24
        }
25
26
        return true;
27
    }
28
}
29