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

AvailablePaymentMethodsCall::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 10
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\Api\Adapter\ApiCall;
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 SprykerEco\Shared\Afterpay\AfterpayApiRequestConfig;
13
use SprykerEco\Zed\Afterpay\AfterpayConfig;
14
use SprykerEco\Zed\Afterpay\Business\Api\Adapter\Client\ClientInterface;
15
use SprykerEco\Zed\Afterpay\Business\Api\Adapter\Converter\TransferToCamelCaseArrayConverterInterface;
16
use SprykerEco\Zed\Afterpay\Business\Exception\ApiHttpRequestException;
17
use SprykerEco\Zed\Afterpay\Dependency\Service\AfterpayToUtilEncodingInterface;
18
19
class AvailablePaymentMethodsCall extends AbstractApiCall implements AvailablePaymentMethodsCallInterface
20
{
21
    /**
22
     * @var \SprykerEco\Zed\Afterpay\Business\Api\Adapter\Client\ClientInterface
23
     */
24
    protected $client;
25
26
    /**
27
     * @var \SprykerEco\Zed\Afterpay\AfterpayConfig
28
     */
29
    private $config;
30
31
    /**
32
     * @param \SprykerEco\Zed\Afterpay\Business\Api\Adapter\Client\ClientInterface $client
33
     * @param \SprykerEco\Zed\Afterpay\Business\Api\Adapter\Converter\TransferToCamelCaseArrayConverterInterface $transferConverter
34
     * @param \SprykerEco\Zed\Afterpay\Dependency\Service\AfterpayToUtilEncodingInterface $utilEncoding
35
     * @param \SprykerEco\Zed\Afterpay\AfterpayConfig $config
36
     */
37
    public function __construct(
38
        ClientInterface $client,
39
        TransferToCamelCaseArrayConverterInterface $transferConverter,
40
        AfterpayToUtilEncodingInterface $utilEncoding,
41
        AfterpayConfig $config
42
    ) {
43
        $this->client = $client;
44
        $this->transferConverter = $transferConverter;
45
        $this->utilEncoding = $utilEncoding;
46
        $this->config = $config;
47
    }
48
49
    /**
50
     * @param \Generated\Shared\Transfer\AfterpayAvailablePaymentMethodsRequestTransfer $requestTransfer
51
     *
52
     * @return \Generated\Shared\Transfer\AfterpayAvailablePaymentMethodsResponseTransfer
53
     */
54
    public function execute(AfterpayAvailablePaymentMethodsRequestTransfer $requestTransfer): AfterpayAvailablePaymentMethodsResponseTransfer
55
    {
56
        $jsonRequest = $this->buildJsonRequestFromTransferObject($requestTransfer);
57
58
        try {
59
            $jsonResponse = $this->client->sendPost(
60
                $this->config->getAvailablePaymentMethodsApiEndpointUrl(),
61
                $jsonRequest
62
            );
63
        } catch (ApiHttpRequestException $apiHttpRequestException) {
64
            $this->logApiException($apiHttpRequestException);
65
            $jsonResponse = '[]';
66
        }
67
68
        return $this->buildAvailablePaymentMethodsResponseTransfer($jsonResponse);
69
    }
70
71
    /**
72
     * @param string $jsonResponse
73
     *
74
     * @return \Generated\Shared\Transfer\AfterpayAvailablePaymentMethodsResponseTransfer
75
     */
76
    protected function buildAvailablePaymentMethodsResponseTransfer(string $jsonResponse): AfterpayAvailablePaymentMethodsResponseTransfer
77
    {
78
        $jsonResponseArray = $this->utilEncoding->decodeJson($jsonResponse, true);
79
80
        $responseTransfer = new AfterpayAvailablePaymentMethodsResponseTransfer();
81
82
        $riskCheckResultCode = $this->extractRiskCheckCode($jsonResponseArray);
0 ignored issues
show
Bug introduced by
It seems like $jsonResponseArray can also be of type null; however, parameter $jsonResponseArray of SprykerEco\Zed\Afterpay\...:extractRiskCheckCode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

82
        $riskCheckResultCode = $this->extractRiskCheckCode(/** @scrutinizer ignore-type */ $jsonResponseArray);
Loading history...
83
        $customerNumber = $this->extractCustomerNumber($jsonResponseArray);
0 ignored issues
show
Bug introduced by
It seems like $jsonResponseArray can also be of type null; however, parameter $jsonResponseArray of SprykerEco\Zed\Afterpay\...extractCustomerNumber() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

83
        $customerNumber = $this->extractCustomerNumber(/** @scrutinizer ignore-type */ $jsonResponseArray);
Loading history...
84
85
        $responseTransfer
86
            ->setCheckoutId($jsonResponseArray[AfterpayApiRequestConfig::TRANSACTION_CHECKOUT_ID] ?? null)
87
            ->setOutcome($jsonResponseArray[AfterpayApiRequestConfig::TRANSACTION_OUTCOME] ?? null)
88
            ->setCustomer($jsonResponseArray[AfterpayApiRequestConfig::CUSTOMER] ?? [])
89
            ->setCustomerNumber($customerNumber)
90
            ->setPaymentMethods($jsonResponseArray[AfterpayApiRequestConfig::PAYMENT_METHODS] ?? [])
91
            ->setRiskCheckResultCode($riskCheckResultCode);
92
93
        return $responseTransfer;
94
    }
95
96
    /**
97
     * @param array $jsonResponseArray
98
     *
99
     * @return string|null
100
     */
101
    protected function extractRiskCheckCode(array $jsonResponseArray): ?string
102
    {
103
        if (!isset($jsonResponseArray[AfterpayApiRequestConfig::ADDITIONAL_RESPONSE_INFO][AfterpayApiRequestConfig::RISK_CHECK_CODE])) {
104
            return null;
105
        }
106
107
        return $jsonResponseArray[AfterpayApiRequestConfig::ADDITIONAL_RESPONSE_INFO][AfterpayApiRequestConfig::RISK_CHECK_CODE];
108
    }
109
110
    /**
111
     * @param array $jsonResponseArray
112
     *
113
     * @return string|null
114
     */
115
    protected function extractCustomerNumber(array $jsonResponseArray): ?string
116
    {
117
        if (!isset($jsonResponseArray[AfterpayApiRequestConfig::CUSTOMER][AfterpayApiRequestConfig::CUSTOMER_NUMBER])) {
118
            return null;
119
        }
120
121
        return $jsonResponseArray[AfterpayApiRequestConfig::CUSTOMER][AfterpayApiRequestConfig::CUSTOMER_NUMBER];
122
    }
123
}
124