WebhooksController::test()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace App\Controller\Braintree;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\Routing\Annotation\Route;
7
use Symfony\Component\HttpFoundation\Response;
8
use Exception;
9
10
/**
11
 * Class WebhooksController
12
 *
13
 * @package App\Controller\Braintree
14
 *
15
 * @Route("/braintree/webhooks", name="braintree-webhooks-")
16
 */
17
class WebhooksController extends AbstractController
18
{
19
    /**ff
20
     * @Route("/", name="index", methods={"POST"})
21
     *
22
     * @return Response
23
     */
24
    public function index(): Response
25
    {
26
        $request = Request::createFromGlobals();
27
        $parameters = $request->request->all();
28
        $btSignature = $parameters['bt_signature'] ?? null;
29
        $btPayload = $parameters['bt_payload'] ?? null;
30
31
        return $this->processWebhook($btSignature, $btPayload);
32
    }
33
34
    /**
35
     * @Route(
36
     *     "/test/local-payment-method",
37
     *     name="local-payment-method",
38
     *     methods={"GET"}, defaults={"action" = "LOCAL_PAYMENT_COMPLETED"})
39
     * @Route(
40
     *     "/test/payment-revoked",
41
     *     name="payment-revoked",
42
     *     methods={"GET"}, defaults={"action" = "PAYMENT_METHOD_REVOKED_BY_CUSTOMER"})
43
     *
44
     * @param string $action
45
     *
46
     * @return Response
47
     */
48
    public function test(string $action): Response
49
    {
50
        $testNotification = $this->webhookService->generateTestNotification($action);
51
        $btSignature = $testNotification['bt_signature'];
52
        $btPayload = $testNotification['bt_payload'];
53
54
        return $this->processWebhook($btSignature, $btPayload);
55
    }
56
57
    /**
58
     * @param $btSignature
59
     * @param $btPayload
60
     * @return Response
61
     */
62
    protected function processWebhook($btSignature, $btPayload): Response
63
    {
64
        try {
65
            $result = $this->webhookService->processWebhook($btSignature, $btPayload);
66
            return $result ? new Response('OK', 201) : new Response('Error processing the webhook', 422);
67
        } catch (Exception $exception) {
68
            error_log($exception->getMessage());
69
            return new Response('Unable to process webhook', 422);
70
        }
71
    }
72
}
73