RatepayPreCheckPlugin::checkForErrors()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 11
rs 10
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Ratepay\Communication\Plugin\Checkout;
9
10
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...
11
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...
12
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...
13
use Generated\Shared\Transfer\RatepayPaymentRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...yPaymentRequestTransfer 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\RatepayResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\RatepayResponseTransfer 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 Spryker\Zed\Kernel\Communication\AbstractPlugin;
16
use Spryker\Zed\Payment\Dependency\Plugin\Checkout\CheckoutPreCheckPluginInterface;
17
18
/**
19
 * @method \SprykerEco\Zed\Ratepay\Business\RatepayFacadeInterface getFacade()
20
 * @method \SprykerEco\Zed\Ratepay\Communication\RatepayCommunicationFactory getFactory()
21
 * @method \SprykerEco\Zed\Ratepay\RatepayConfig getConfig()
22
 * @method \SprykerEco\Zed\Ratepay\Persistence\RatepayQueryContainerInterface getQueryContainer()
23
 */
24
class RatepayPreCheckPlugin extends AbstractPlugin implements CheckoutPreCheckPluginInterface
25
{
26
    /**
27
     * @api
28
     *
29
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
30
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponse
31
     *
32
     * @return \Generated\Shared\Transfer\CheckoutResponseTransfer
33
     */
34
    public function execute(QuoteTransfer $quoteTransfer, CheckoutResponseTransfer $checkoutResponse)
35
    {
36
        return $this->checkCondition($quoteTransfer, $checkoutResponse);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->checkCondi...fer, $checkoutResponse) returns the type Generated\Shared\Transfer\CheckoutResponseTransfer which is incompatible with the return type mandated by Spryker\Zed\Payment\Depe...ginInterface::execute() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
37
    }
38
39
    /**
40
     * @api
41
     *
42
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
43
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
44
     *
45
     * @return \Generated\Shared\Transfer\CheckoutResponseTransfer
46
     */
47
    public function checkCondition(
48
        QuoteTransfer $quoteTransfer,
49
        CheckoutResponseTransfer $checkoutResponseTransfer
50
    ) {
51
        $ratepayPaymentInitTransfer = $this->getFactory()->createPaymentInitTransfer();
52
        $quotePaymentInitMapper = $this->getFactory()->createPaymentInitMapperByQuote(
53
            $ratepayPaymentInitTransfer,
54
            $quoteTransfer
55
        );
56
        $quotePaymentInitMapper->map();
57
58
        $ratepayResponseTransfer = $this->getFacade()->initPayment($ratepayPaymentInitTransfer);
59
        $paymentData = $this->getFactory()
60
            ->getPaymentMethodExtractor()
61
            ->extractPaymentMethod($quoteTransfer);
62
        if ($paymentData) {
63
            $paymentData
64
                ->setTransactionId($ratepayResponseTransfer->getTransactionId())
0 ignored issues
show
Bug introduced by
The method setTransactionId() does not exist on Spryker\Shared\Kernel\Transfer\AbstractTransfer. ( Ignorable by Annotation )

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

64
                ->/** @scrutinizer ignore-call */ 
65
                  setTransactionId($ratepayResponseTransfer->getTransactionId())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
                ->setTransactionShortId($ratepayResponseTransfer->getTransactionShortId())
66
                ->setResultCode($ratepayResponseTransfer->getStatusCode());
67
        }
68
69
        $partialOrderTransfer = $this->getPartialOrderTransferByBasketItems($quoteTransfer);
70
71
        $ratepayPaymentRequestTransfer = new RatepayPaymentRequestTransfer();
72
        $quotePaymentInitMapper = $this->getFactory()->createPaymentRequestMapperByQuote(
73
            $ratepayPaymentRequestTransfer,
74
            $ratepayPaymentInitTransfer,
75
            $quoteTransfer,
76
            $partialOrderTransfer,
77
            $paymentData
78
        );
79
        $quotePaymentInitMapper->map();
80
81
        $ratepayResponseTransfer = $this->getFacade()->requestPayment($ratepayPaymentRequestTransfer);
82
83
        $this->getFacade()->updatePaymentMethodByPaymentResponse(
84
            $ratepayResponseTransfer,
85
            $ratepayPaymentRequestTransfer->getOrderId()
86
        );
87
88
        $this->checkForErrors($ratepayResponseTransfer, $checkoutResponseTransfer);
89
    }
90
91
    /**
92
     * @param \Generated\Shared\Transfer\RatepayResponseTransfer $ratepayResponseTransfer
93
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
94
     *
95
     * @return void
96
     */
97
    protected function checkForErrors(RatepayResponseTransfer $ratepayResponseTransfer, CheckoutResponseTransfer $checkoutResponseTransfer)
98
    {
99
        if (!$ratepayResponseTransfer->getSuccessful()) {
100
            $errorMessage = $ratepayResponseTransfer->getCustomerMessage() != '' ? $ratepayResponseTransfer->getCustomerMessage() :
101
                $ratepayResponseTransfer->getResultText();
102
103
            $error = new CheckoutErrorTransfer();
104
            $error
105
                ->setErrorCode($ratepayResponseTransfer->getResultCode())
106
                ->setMessage($errorMessage);
107
            $checkoutResponseTransfer->addError($error);
108
        }
109
    }
110
111
    /**
112
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
113
     *
114
     * @return \Generated\Shared\Transfer\OrderTransfer
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\OrderTransfer 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...
115
     */
116
    protected function getPartialOrderTransferByBasketItems($quoteTransfer)
117
    {
118
        $partialOrderTransfer = $this->getFactory()->createOrderTransfer();
119
        $items = $this->getFactory()->createOrderTransferItemsByBasketItems($quoteTransfer->getItems());
120
        $partialOrderTransfer->setItems($items);
121
        $partialOrderTransfer->setPriceMode($quoteTransfer->getPriceMode());
122
        $partialOrderTransfer->setTotals($quoteTransfer->getTotals());
123
124
        return $this
125
            ->getFactory()
126
            ->getCalculationFacade()
127
            ->getOrderTotalByOrderTransfer($partialOrderTransfer);
128
    }
129
}
130