Completed
Pull Request — master (#9)
by Cesar
02:06
created

PaymentsController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 115
Duplicated Lines 21.74 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 25
loc 115
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A dropUI() 8 8 1
A hostedFields() 8 8 1
A paypal() 9 9 1
A paymentsPayload() 0 9 1
A createTransaction() 0 15 1
A captureTransaction() 0 10 1
A getTransaction() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace App\Controller\Braintree;
4
5
use Braintree\Exception\NotFound;
6
use Braintree\Transaction;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\Routing\Annotation\Route;
9
use Symfony\Component\HttpFoundation\Response;
10
11
/**
12
 * Class PaymentsController
13
 *
14
 * @package App\Controller\Braintree
15
 *
16
 * @Route("/braintree/payments", name="braintree-payments-")
17
 */
18
class PaymentsController extends AbstractController
19
{
20
    /**
21
     * @Route("/dropui", name="dropui", methods={"GET"})
22
     *
23
     * @return Response
24
     */
25 View Code Duplication
    public function dropUI()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
    {
27
        $clientToken = $this->braintreeService->getPaymentService()->getClientToken();
28
        return $this->render('braintree/payments/dropui.html.twig', [
29
            'clientToken' => $clientToken,
30
            'name' => 'Drop UI'
31
        ]);
32
    }
33
34
    /**
35
     * @Route("/hosted-fields", name="hosted-fields", methods={"GET"})
36
     *
37
     * @return Response
38
     */
39 View Code Duplication
    public function hostedFields()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41
        $clientToken = $this->braintreeService->getPaymentService()->getClientToken();
42
        return $this->render('braintree/payments/hosted-fields.html.twig', [
43
            'clientToken' => $clientToken,
44
            'name' => 'Hosted Fields'
45
        ]);
46
    }
47
48
    /**
49
     * @Route("/apm", name="apm", methods={"GET"})
50
     *
51
     * @return Response
52
     */
53 View Code Duplication
    public function paypal()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55
        $clientToken = $this->braintreeService->getPaymentService()->getClientToken();
56
        return $this->render('braintree/payments/apm.html.twig', [
57
            'clientToken' => $clientToken,
58
            'name' => 'DropUI + APM',
59
            'paypalClientId' => $this->getParameter('PAYPAL_SDK_CLIENT_ID'),
60
        ]);
61
    }
62
63
    /**
64
     * @Route("/payload", name="payload", methods={"POST"})
65
     *
66
     * @return Response
67
     */
68
    public function paymentsPayload()
69
    {
70
        $request = Request::createFromGlobals();
71
        $payload = $request->request->all();
72
        return $this->render('default/dump.html.twig', [
73
            'result' => (object) $payload,
74
            'raw_result' => false,
75
        ]);
76
    }
77
78
    /**
79
     * @Route("/transaction", name="create", methods={"POST"})
80
     *
81
     * @return Response
82
     */
83
    public function createTransaction()
84
    {
85
        $request = Request::createFromGlobals();
86
        $paymentNonce = $request->request->get('payment_nonce');
87
        $amount = $request->request->get('amount');
88
        $deviceData = $request->request->get('device_data');
89
        $sale = $this->braintreeService->getPaymentService()->createSale($amount, $paymentNonce, $deviceData);
90
        /** @var Transaction $transaction */
91
        $transaction = $sale->transaction;
92
        return $this->render('default/dump-input-id.html.twig', [
93
            'result' => $sale,
94
            'raw_result' => false,
95
            'result_id' => $transaction->id
96
        ]);
97
    }
98
99
    /**
100
     * @Route("/transaction/{transactionId}/capture", name="capture", methods={"POST"})
101
     *
102
     * @param string $transactionId
103
     *
104
     * @return Response
105
     */
106
    public function captureTransaction(string $transactionId)
107
    {
108
        $request = Request::createFromGlobals();
109
        $amount = $request->request->get('amount');
110
        $capture = $this->braintreeService->getPaymentService()->captureSale($transactionId, $amount);
111
        return $this->render('default/dump.html.twig', [
112
            'result' => (object) $capture,
113
            'raw_result' => false,
114
        ]);
115
    }
116
117
    /**
118
     * @Route("/{transactionId}", name="get", methods={"GET"})
119
     *
120
     * @param string $transactionId
121
     * @return Response
122
     * @throws NotFound
123
     */
124
    public function getTransaction(string $transactionId)
125
    {
126
        $transaction = $this->braintreeService->getPaymentService()->getTransaction($transactionId);
127
        return $this->render('default/dump.html.twig', [
128
            'result' => (object) $transaction,
129
            'raw_result' => false,
130
        ]);
131
    }
132
}
133