CompletePurchaseMiddleware::handle()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 3
nc 3
nop 3
1
<?php
2
/**
3
 * @link https://github.com/phpviet/laravel-omnipay
4
 *
5
 * @copyright (c) PHP Viet
6
 * @license [MIT](https://opensource.org/licenses/MIT)
7
 */
8
9
namespace PHPViet\Laravel\Omnipay\Middleware;
10
11
use Closure;
12
use Omnipay\Common\AbstractGateway;
13
use Omnipay\Common\Exception\OmnipayException;
14
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
15
16
/**
17
 * @author Vuong Minh <[email protected]>
18
 * @since 1.1.0
19
 */
20
class CompletePurchaseMiddleware
21
{
22
    /**
23
     * @param \Illuminate\Http\Request $request
24
     * @param \Closure $next
25
     * @return mixed
26
     */
27
    public function handle($request, Closure $next, string $gateway)
28
    {
29
        /** @var AbstractGateway $gateway */
30
        $gateway = app('omnipay')->gateway($gateway);
31
32
        if (! $gateway->supportsCompletePurchase()) {
33
            throw new \InvalidArgumentException('Gateway configured not support complete purchase method!');
34
        }
35
36
        try {
37
            $response = $gateway->completePurchase()->send();
0 ignored issues
show
Bug introduced by
The method completePurchase() does not exist on Omnipay\Common\AbstractGateway. Did you maybe mean supportsCompletePurchase()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
38
        } catch (OmnipayException $e) {
39
            throw new BadRequestHttpException($e->getMessage());
40
        }
41
42
        $request->attributes->set('completePurchaseResponse', $response);
43
44
        return $next($request);
45
    }
46
}
47