BraintreeHandler::setBraintreePayment()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 4
nop 3
dl 0
loc 25
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Yves\Braintree\Handler;
9
10
use Generated\Shared\Transfer\QuoteTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\QuoteTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Spryker\Yves\Currency\Plugin\CurrencyPluginInterface;
12
use SprykerEco\Shared\Braintree\BraintreeConfig as BraintreeSharedConfig;
13
use SprykerEco\Yves\Braintree\BraintreeConfig;
14
use Symfony\Component\HttpFoundation\Request;
15
16
class BraintreeHandler implements BraintreeHandlerInterface
17
{
18
    public const PAYMENT_PROVIDER = 'braintree';
19
    public const PAYMENT_METHOD_NONCE = 'payment_method_nonce';
20
21
    /**
22
     * @var array
23
     */
24
    protected static $paymentMethods = [
25
        BraintreeSharedConfig::PAYMENT_METHOD_PAY_PAL => 'pay_pal',
26
        BraintreeSharedConfig::PAYMENT_METHOD_CREDIT_CARD => 'credit_card',
27
    ];
28
29
    /**
30
     * @var \Spryker\Yves\Currency\Plugin\CurrencyPluginInterface
31
     */
32
    protected $currencyPlugin;
33
34
    /**
35
     * @var \SprykerEco\Yves\Braintree\BraintreeConfig $braintreeConfig
36
     */
37
    protected $braintreeConfig;
38
39
    /**
40
     * @param \Spryker\Yves\Currency\Plugin\CurrencyPluginInterface $currencyPlugin
41
     * @param \SprykerEco\Yves\Braintree\BraintreeConfig $braintreeConfig
42
     */
43
    public function __construct(CurrencyPluginInterface $currencyPlugin, BraintreeConfig $braintreeConfig)
44
    {
45
        $this->currencyPlugin = $currencyPlugin;
46
        $this->braintreeConfig = $braintreeConfig;
47
    }
48
49
    /**
50
     * @param \Symfony\Component\HttpFoundation\Request $request
51
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
52
     *
53
     * @return \Generated\Shared\Transfer\QuoteTransfer
54
     */
55
    public function addPaymentToQuote(Request $request, QuoteTransfer $quoteTransfer)
56
    {
57
        $paymentSelection = $quoteTransfer->getPayment()->getPaymentSelection();
58
59
        $this->setPaymentProviderAndMethod($quoteTransfer, $paymentSelection);
60
        $this->setBraintreePayment($request, $quoteTransfer, $paymentSelection);
61
62
        return $quoteTransfer;
63
    }
64
65
    /**
66
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
67
     * @param string $paymentSelection
68
     *
69
     * @return void
70
     */
71
    protected function setPaymentProviderAndMethod(QuoteTransfer $quoteTransfer, $paymentSelection)
72
    {
73
        $quoteTransfer->getPayment()
74
            ->setPaymentProvider(BraintreeSharedConfig::PROVIDER_NAME)
75
            ->setPaymentMethod(static::$paymentMethods[$paymentSelection]);
76
    }
77
78
    /**
79
     * @param \Symfony\Component\HttpFoundation\Request $request
80
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
81
     * @param string $paymentSelection
82
     *
83
     * @return void
84
     */
85
    protected function setBraintreePayment(Request $request, QuoteTransfer $quoteTransfer, $paymentSelection)
86
    {
87
        $braintreePaymentTransfer = $this->getBraintreePaymentTransfer($quoteTransfer, $paymentSelection);
88
        $nonce = $request->request->get(self::PAYMENT_METHOD_NONCE);
89
90
        if ($this->braintreeConfig->getFakePaymentMethodNonce()) {
91
            $nonce = $this->braintreeConfig->getFakePaymentMethodNonce();
92
        }
93
94
        if ($nonce === null) {
95
            return;
96
        }
97
98
        $billingAddress = $quoteTransfer->getBillingAddress();
99
        $braintreePaymentTransfer
100
            ->setAccountBrand(static::$paymentMethods[$paymentSelection])
101
            ->setBillingAddress($billingAddress)
102
            ->setShippingAddress($quoteTransfer->getShippingAddress())
103
            ->setEmail($quoteTransfer->getCustomer()->getEmail())
104
            ->setCurrencyIso3Code($this->getCurrency())
105
            ->setLanguageIso2Code($billingAddress->getIso2Code())
106
            ->setClientIp($request->getClientIp())
107
            ->setNonce($nonce);
108
109
        $quoteTransfer->getPayment()->setBraintree(clone $braintreePaymentTransfer);
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    protected function getCurrency()
116
    {
117
        return $this->currencyPlugin->getCurrent()->getCode();
118
    }
119
120
    /**
121
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
122
     * @param string $paymentSelection
123
     *
124
     * @return \Generated\Shared\Transfer\BraintreePaymentTransfer
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\BraintreePaymentTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
125
     */
126
    protected function getBraintreePaymentTransfer(QuoteTransfer $quoteTransfer, $paymentSelection)
127
    {
128
        $method = 'get' . ucfirst($paymentSelection);
129
        $braintreePaymentTransfer = $quoteTransfer->getPayment()->$method();
130
131
        return $braintreePaymentTransfer;
132
    }
133
}
134