AfterPayPaymentMethodsProvider   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A sendRequest() 0 3 1
A buildRequestTransferFromQuote() 0 3 1
A getAvailablePaymentMethods() 0 12 1
A fetchAvailablePaymentMethodsNames() 0 11 3
A parseResponseTransfer() 0 11 1
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\Filter\Provider;
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 AfterPayPaymentMethodsProvider implements AfterPayPaymentMethodsProviderInterface
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
        $availablePaymentMethodNames = $this->fetchAvailablePaymentMethodsNames($apiResponseTransfer);
88
89
        return (new AfterPayAvailablePaymentMethodsTransfer())
90
            ->setAvailablePaymentMethodNames($availablePaymentMethodNames)
91
            ->setRiskCheckCode($apiResponseTransfer->getRiskCheckResultCode())
92
            ->setCheckoutId($apiResponseTransfer->getCheckoutId())
93
            ->setCustomerNumber($apiResponseTransfer->getCustomerNumber())
94
            ->setOutcome($apiResponseTransfer->getOutcome())
95
            ->setRiskCheckMessages($apiResponseTransfer->getRiskCheckMessages());
96
    }
97
98
    /**
99
     * @param \Generated\Shared\Transfer\AfterPayAvailablePaymentMethodsResponseTransfer $apiResponseTransfer
100
     *
101
     * @return array
102
     */
103
    protected function fetchAvailablePaymentMethodsNames(AfterPayAvailablePaymentMethodsResponseTransfer $apiResponseTransfer): array
104
    {
105
        $availablePaymentMethodNames = [];
106
        foreach ($apiResponseTransfer->getPaymentMethods() as $paymentMethodArray) {
107
            if (!isset($paymentMethodArray['type'])) {
108
                continue;
109
            }
110
            $availablePaymentMethodNames[] = $paymentMethodArray['type'];
111
        }
112
113
        return array_unique($availablePaymentMethodNames);
114
    }
115
}
116