Passed
Push — feature/eco-574/eco-2266-check... ( efd21d )
by Aleksey
08:13
created

fetchAvailablePaymentMethodsNames()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 11
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\Afterpay\Business\Payment\Handler\RiskCheck;
9
10
use Generated\Shared\Transfer\AfterpayAvailablePaymentMethodsRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...tMethodsRequestTransfer 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\AfterpayAvailablePaymentMethodsResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...MethodsResponseTransfer 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\AfterpayAvailablePaymentMethodsTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ePaymentMethodsTransfer 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\Afterpay\Business\Api\Adapter\AdapterInterface;
15
use SprykerEco\Zed\Afterpay\Business\Payment\Mapper\QuoteToRequestTransferInterface;
16
17
class AvailablePaymentMethodsHandler implements AvailablePaymentMethodsHandlerInterface
18
{
19
    /**
20
     * @var \SprykerEco\Zed\Afterpay\Business\Api\Adapter\AdapterInterface
21
     */
22
    protected $apiAdapter;
23
24
    /**
25
     * @var \SprykerEco\Zed\Afterpay\Business\Payment\Mapper\QuoteToRequestTransferInterface
26
     */
27
    protected $quoteToRequestMapper;
28
29
    /**
30
     * @param \SprykerEco\Zed\Afterpay\Business\Api\Adapter\AdapterInterface $apiAdapter
31
     * @param \SprykerEco\Zed\Afterpay\Business\Payment\Mapper\QuoteToRequestTransferInterface $quoteToRequestMapper
32
     */
33
    public function __construct(
34
        AdapterInterface $apiAdapter,
35
        QuoteToRequestTransferInterface $quoteToRequestMapper
36
    ) {
37
        $this->apiAdapter = $apiAdapter;
38
        $this->quoteToRequestMapper = $quoteToRequestMapper;
39
    }
40
41
    /**
42
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
43
     *
44
     * @return \Generated\Shared\Transfer\AfterpayAvailablePaymentMethodsTransfer
45
     */
46
    public function getAvailablePaymentMethods(QuoteTransfer $quoteTransfer): AfterpayAvailablePaymentMethodsTransfer
47
    {
48
        $requestTransfer = $this->buildRequestTransferFromQuote($quoteTransfer);
49
        $responseTransfer = $this->sendRequest($requestTransfer);
50
51
        $availablePaymentMethodsTransfer = $this->parseResponseTransfer($responseTransfer);
52
53
        $availablePaymentMethodsTransfer->setQuoteHash(
54
            $quoteTransfer->getTotals()->getHash()
55
        );
56
57
        return $availablePaymentMethodsTransfer;
58
    }
59
60
    /**
61
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
62
     *
63
     * @return \Generated\Shared\Transfer\AfterpayAvailablePaymentMethodsRequestTransfer
64
     */
65
    protected function buildRequestTransferFromQuote(QuoteTransfer $quoteTransfer): AfterpayAvailablePaymentMethodsRequestTransfer
66
    {
67
        return $this->quoteToRequestMapper->quoteToAvailablePaymentMethods($quoteTransfer);
68
    }
69
70
    /**
71
     * @param \Generated\Shared\Transfer\AfterpayAvailablePaymentMethodsRequestTransfer $requestTransfer
72
     *
73
     * @return \Generated\Shared\Transfer\AfterpayAvailablePaymentMethodsResponseTransfer
74
     */
75
    protected function sendRequest(AfterpayAvailablePaymentMethodsRequestTransfer $requestTransfer): AfterpayAvailablePaymentMethodsResponseTransfer
76
    {
77
        return $this->apiAdapter->sendAvailablePaymentMethodsRequest($requestTransfer);
78
    }
79
80
    /**
81
     * @param \Generated\Shared\Transfer\AfterpayAvailablePaymentMethodsResponseTransfer $apiResponseTransfer
82
     *
83
     * @return \Generated\Shared\Transfer\AfterpayAvailablePaymentMethodsTransfer
84
     */
85
    protected function parseResponseTransfer(AfterpayAvailablePaymentMethodsResponseTransfer $apiResponseTransfer): AfterpayAvailablePaymentMethodsTransfer
86
    {
87
        $availablePaymentMethodsTransfer = new AfterpayAvailablePaymentMethodsTransfer();
88
89
        $availablePaymentMethodNames = $this->fetchAvailablePaymentMethodsNames($apiResponseTransfer);
90
91
        $availablePaymentMethodsTransfer
92
            ->setAvailablePaymentMethodNames($availablePaymentMethodNames)
93
            ->setRiskCheckCode($apiResponseTransfer->getRiskCheckResultCode())
94
            ->setCheckoutId($apiResponseTransfer->getCheckoutId())
95
            ->setCustomerNumber($apiResponseTransfer->getCustomerNumber());
96
97
        return $availablePaymentMethodsTransfer;
98
    }
99
100
    /**
101
     * @param \Generated\Shared\Transfer\AfterpayAvailablePaymentMethodsResponseTransfer $apiResponseTransfer
102
     *
103
     * @return array
104
     */
105
    protected function fetchAvailablePaymentMethodsNames(AfterpayAvailablePaymentMethodsResponseTransfer $apiResponseTransfer): array
106
    {
107
        $availablePaymentMethodNames = [];
108
        foreach ($apiResponseTransfer->getPaymentMethods() as $paymentMethodArray) {
109
            if (!isset($paymentMethodArray['type'])) {
110
                continue;
111
            }
112
            $availablePaymentMethodNames[] = $paymentMethodArray['type'];
113
        }
114
115
        return array_unique($availablePaymentMethodNames);
116
    }
117
}
118