BraintreePreCheckPlugin   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 20
c 3
b 0
f 1
dl 0
loc 62
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A checkCondition() 0 21 3
A checkForErrors() 0 17 3
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\Zed\Braintree\Communication\Plugin\Checkout;
9
10
use Generated\Shared\Transfer\BraintreeTransactionResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...sactionResponseTransfer 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 Generated\Shared\Transfer\CheckoutErrorTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\CheckoutErrorTransfer 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...
12
use Generated\Shared\Transfer\CheckoutResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\CheckoutResponseTransfer 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...
13
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...
14
use Spryker\Zed\Checkout\Dependency\Plugin\CheckoutPreConditionInterface;
15
use Spryker\Zed\Kernel\Communication\AbstractPlugin as BaseAbstractPlugin;
16
use SprykerEco\Shared\Braintree\BraintreeConfig;
17
18
/**
19
 * @method \SprykerEco\Zed\Braintree\Business\BraintreeFacadeInterface getFacade()
20
 */
21
class BraintreePreCheckPlugin extends BaseAbstractPlugin implements CheckoutPreConditionInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Spryker\Zed\Checkout\Dep...utPreConditionInterface has been deprecated: Use {@link \Spryker\Zed\CheckoutExtension\Dependency\Plugin\CheckoutPreConditionInterface} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

21
class BraintreePreCheckPlugin extends BaseAbstractPlugin implements /** @scrutinizer ignore-deprecated */ CheckoutPreConditionInterface

This interface has been deprecated. The supplier of the interface has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.

Loading history...
22
{
23
    /**
24
     * Specification:
25
     * - Checks a condition before the order is saved. If the condition fails, an error is added to the response transfer and 'false' is returned.
26
     * - Check could be passed (returns 'true') along with errors added to the checkout response.
27
     * - Quote transfer should not be changed
28
     * - Don't use this plugin to write to a DB
29
     *
30
     * @api
31
     *
32
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
33
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
34
     *
35
     * @return bool
36
     */
37
    public function checkCondition(
38
        QuoteTransfer $quoteTransfer,
39
        CheckoutResponseTransfer $checkoutResponseTransfer
40
    ) {
41
        if ($quoteTransfer->getPayment()->getPaymentProvider() !== BraintreeConfig::PROVIDER_NAME) {
42
            return true;
43
        }
44
45
        $braintreeTransactionResponseTransfer = $this->getFacade()->preCheckPayment($quoteTransfer);
46
        $isPassed = $this->checkForErrors($braintreeTransactionResponseTransfer, $checkoutResponseTransfer);
47
48
        if (!$braintreeTransactionResponseTransfer->getIsSuccess()) {
49
            return false;
50
        }
51
52
        $quoteTransfer->getPayment()->getBraintree()
53
            ->setTransactionId($braintreeTransactionResponseTransfer->getTransactionId());
54
55
        $quoteTransfer->getPayment()->setBraintreeTransactionResponse($braintreeTransactionResponseTransfer);
56
57
        return $isPassed;
58
    }
59
60
    /**
61
     * @param \Generated\Shared\Transfer\BraintreeTransactionResponseTransfer $braintreeTransactionResponseTransfer
62
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
63
     *
64
     * @return bool
65
     */
66
    protected function checkForErrors(
67
        BraintreeTransactionResponseTransfer $braintreeTransactionResponseTransfer,
68
        CheckoutResponseTransfer $checkoutResponseTransfer
69
    ) {
70
        if ($braintreeTransactionResponseTransfer->getIsSuccess()) {
71
            return true;
72
        }
73
74
        $errorCode = $braintreeTransactionResponseTransfer->getCode() ?: 500;
75
        $error = new CheckoutErrorTransfer();
76
77
        $error
78
            ->setErrorCode($errorCode)
79
            ->setMessage($braintreeTransactionResponseTransfer->getMessage());
80
        $checkoutResponseTransfer->addError($error);
81
82
        return false;
83
    }
84
}
85