Completed
Pull Request — master (#34)
by Roman
14:40 queued 07:36
created

CheckoutPostSaveHook::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 6
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\Hook;
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 SprykerEco\Zed\Braintree\Business\Order\SaverInterface;
15
use SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\PaymentTransactionHandlerInterface;
16
use Symfony\Component\HttpFoundation\Response;
17
18
class CheckoutPostSaveHook implements CheckoutPostSaveHookInterface
19
{
20
    /**
21
     * @var \SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\PaymentTransactionHandlerInterface
22
     */
23
    protected $paymentTransactionHandler;
24
25
    /**
26
     * @var \SprykerEco\Zed\Braintree\Business\Order\SaverInterface
27
     */
28
    protected $orderSaver;
29
30
    /**
31
     * @param \SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\PaymentTransactionHandlerInterface $paymentTransactionHandler
32
     * @param \SprykerEco\Zed\Braintree\Business\Order\SaverInterface $orderSaver
33
     */
34
    public function __construct(
35
        PaymentTransactionHandlerInterface $paymentTransactionHandler,
36
        SaverInterface $orderSaver
37
    ) {
38
        $this->paymentTransactionHandler = $paymentTransactionHandler;
39
        $this->orderSaver = $orderSaver;
40
    }
41
42
    /**
43
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
44
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
45
     *
46
     * @return \Generated\Shared\Transfer\CheckoutResponseTransfer
47
     */
48
    public function executeCheckoutPostSaveHook(
49
        QuoteTransfer $quoteTransfer,
50
        CheckoutResponseTransfer $checkoutResponseTransfer
51
    ): CheckoutResponseTransfer {
52
        $quoteTransfer = $this->paymentTransactionHandler->createPayment($quoteTransfer, $checkoutResponseTransfer);
53
54
        $braintreeTransactionResponseTransfer = $quoteTransfer->getPayment()->getBraintreeTransactionResponse();
55
        if (!$braintreeTransactionResponseTransfer->getIsSuccess()) {
56
            $checkoutErrorTransfer = $this->buildCheckoutErrorTransfer($braintreeTransactionResponseTransfer);
57
            $checkoutResponseTransfer->addError($checkoutErrorTransfer);
58
59
            return $checkoutResponseTransfer;
60
        }
61
62
        $this->orderSaver->updateOrderPayment($quoteTransfer, $checkoutResponseTransfer->getSaveOrder());
63
64
        return $checkoutResponseTransfer;
65
    }
66
67
    /**
68
     * @param \Generated\Shared\Transfer\BraintreeTransactionResponseTransfer $braintreeTransactionResponseTransfer
69
     *
70
     * @return \Generated\Shared\Transfer\CheckoutErrorTransfer
71
     */
72
    protected function buildCheckoutErrorTransfer(BraintreeTransactionResponseTransfer $braintreeTransactionResponseTransfer): CheckoutErrorTransfer
73
    {
74
        $errorCode = $braintreeTransactionResponseTransfer->getCode() ?: Response::HTTP_INTERNAL_SERVER_ERROR;
75
        $checkoutErrorTransfer = new CheckoutErrorTransfer();
76
77
        $checkoutErrorTransfer->setErrorCode($errorCode)
78
            ->setMessage($braintreeTransactionResponseTransfer->getMessage());
79
80
        return $checkoutErrorTransfer;
81
    }
82
}
83