Passed
Push — bugfix/eco-3539-braintree ( dd0d70...e2e19d )
by Roman
05:31
created

buildCheckoutErrorTransfer()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 1
dl 0
loc 8
rs 10
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\Zed\Braintree\Business\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\PaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PaymentTransfer 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 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...
15
use SprykerEco\Shared\Braintree\BraintreeConfig;
16
use SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\PreCheckTransactionHandlerInterface;
17
use Symfony\Component\HttpFoundation\Response;
18
use ArrayObject;
19
20
class CheckoutPaymentPluginExecutor implements CheckoutPaymentPluginExecutorInterface
21
{
22
    /**
23
     * @var \SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\PreCheckTransactionHandlerInterface
24
     */
25
    protected $preCheckTransactionHandler;
26
27
    /**
28
     * @param \SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\PreCheckTransactionHandlerInterface $preCheckTransactionHandler
29
     */
30
    public function __construct(PreCheckTransactionHandlerInterface $preCheckTransactionHandler) {
31
        $this->preCheckTransactionHandler = $preCheckTransactionHandler;
32
    }
33
34
    /**
35
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
36
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
37
     *
38
     * @return bool
39
     */
40
    public function executePreCheckPlugin(
41
        QuoteTransfer $quoteTransfer,
42
        CheckoutResponseTransfer $checkoutResponseTransfer
43
    ): bool {
44
        $paymentTransfer = $quoteTransfer
45
            ->requirePayment()
46
            ->getPayment();
47
48
        if ($paymentTransfer->getPaymentProvider() !== BraintreeConfig::PROVIDER_NAME) {
49
            return true;
50
        }
51
52
        $braintreeTransactionResponseTransfer = $this->preCheckTransactionHandler->preCheck($quoteTransfer);
53
        if (!$braintreeTransactionResponseTransfer->getIsSuccess()) {
54
            $checkoutErrorTransfer = $this->buildCheckoutErrorTransfer($braintreeTransactionResponseTransfer);
55
            $checkoutResponseTransfer->addError($checkoutErrorTransfer);
56
57
            return false;
58
        }
59
60
        $paymentTransfer->getBraintree()
61
            ->setTransactionId($braintreeTransactionResponseTransfer->getTransactionId());
62
63
        $paymentTransfer->setBraintreeTransactionResponse($braintreeTransactionResponseTransfer);
64
        $quoteTransfer->setPayment($paymentTransfer);
65
66
        return true;
67
    }
68
69
    /**
70
     * @param \Generated\Shared\Transfer\BraintreeTransactionResponseTransfer $braintreeTransactionResponseTransfer
71
     *
72
     * @return CheckoutErrorTransfer
73
     */
74
    protected function buildCheckoutErrorTransfer(BraintreeTransactionResponseTransfer $braintreeTransactionResponseTransfer): CheckoutErrorTransfer {
75
        $errorCode = $braintreeTransactionResponseTransfer->getCode() ?: Response::HTTP_INTERNAL_SERVER_ERROR;
76
        $checkoutErrorTransfer = new CheckoutErrorTransfer();
77
78
        $checkoutErrorTransfer->setErrorCode($errorCode)
79
            ->setMessage($braintreeTransactionResponseTransfer->getMessage());
80
81
        return $checkoutErrorTransfer;
82
    }
83
}
84