extractRiskCheckMessages()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 17
rs 9.9332
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 ArrayObject;
11
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...
12
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...
13
use Generated\Shared\Transfer\AfterPayRiskCheckMessageTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...iskCheckMessageTransfer 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\Shared\AfterPay\AfterPayApiRequestConfig;
15
use SprykerEco\Zed\AfterPay\AfterPayConfig;
16
use SprykerEco\Zed\AfterPay\Business\Api\Adapter\Client\ClientInterface;
17
use SprykerEco\Zed\AfterPay\Business\Api\Adapter\Converter\TransferToCamelCaseArrayConverterInterface;
18
use SprykerEco\Zed\AfterPay\Business\Exception\ApiHttpRequestException;
19
use SprykerEco\Zed\AfterPay\Dependency\Service\AfterPayToUtilEncodingServiceInterface;
20
21
class AvailablePaymentMethodsCall extends AbstractApiCall implements AvailablePaymentMethodsCallInterface
22
{
23
    /**
24
     * @var \SprykerEco\Zed\AfterPay\Business\Api\Adapter\Client\ClientInterface
25
     */
26
    protected $client;
27
28
    /**
29
     * @var \SprykerEco\Zed\AfterPay\AfterPayConfig
30
     */
31
    protected $config;
32
33
    /**
34
     * @param \SprykerEco\Zed\AfterPay\Business\Api\Adapter\Client\ClientInterface $client
35
     * @param \SprykerEco\Zed\AfterPay\Business\Api\Adapter\Converter\TransferToCamelCaseArrayConverterInterface $transferConverter
36
     * @param \SprykerEco\Zed\AfterPay\Dependency\Service\AfterPayToUtilEncodingServiceInterface $utilEncoding
37
     * @param \SprykerEco\Zed\AfterPay\AfterPayConfig $config
38
     */
39
    public function __construct(
40
        ClientInterface $client,
41
        TransferToCamelCaseArrayConverterInterface $transferConverter,
42
        AfterPayToUtilEncodingServiceInterface $utilEncoding,
43
        AfterPayConfig $config
44
    ) {
45
        $this->client = $client;
46
        $this->transferConverter = $transferConverter;
47
        $this->utilEncoding = $utilEncoding;
48
        $this->config = $config;
49
    }
50
51
    /**
52
     * @param \Generated\Shared\Transfer\AfterPayAvailablePaymentMethodsRequestTransfer $requestTransfer
53
     *
54
     * @return \Generated\Shared\Transfer\AfterPayAvailablePaymentMethodsResponseTransfer
55
     */
56
    public function execute(AfterPayAvailablePaymentMethodsRequestTransfer $requestTransfer): AfterPayAvailablePaymentMethodsResponseTransfer
57
    {
58
        $jsonRequest = $this->buildJsonRequestFromTransferObject($requestTransfer);
59
60
        try {
61
            $jsonResponse = $this->client->sendPost(
62
                $this->config->getAvailablePaymentMethodsApiEndpointUrl(),
63
                $jsonRequest,
64
            );
65
        } catch (ApiHttpRequestException $apiHttpRequestException) {
66
            $this->logApiException($apiHttpRequestException);
67
            $jsonResponse = '[]';
68
        }
69
70
        return $this->buildAvailablePaymentMethodsResponseTransfer($jsonResponse);
71
    }
72
73
    /**
74
     * @param string $jsonResponse
75
     *
76
     * @return \Generated\Shared\Transfer\AfterPayAvailablePaymentMethodsResponseTransfer
77
     */
78
    protected function buildAvailablePaymentMethodsResponseTransfer(string $jsonResponse): AfterPayAvailablePaymentMethodsResponseTransfer
79
    {
80
        $jsonResponseArray = $this->utilEncoding->decodeJson($jsonResponse, true);
81
        $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

81
        $riskCheckResultCode = $this->extractRiskCheckCode(/** @scrutinizer ignore-type */ $jsonResponseArray);
Loading history...
82
        $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

82
        $customerNumber = $this->extractCustomerNumber(/** @scrutinizer ignore-type */ $jsonResponseArray);
Loading history...
83
84
        return (new AfterPayAvailablePaymentMethodsResponseTransfer())
85
            ->setCheckoutId($jsonResponseArray[AfterPayApiRequestConfig::TRANSACTION_CHECKOUT_ID] ?? null)
86
            ->setOutcome($jsonResponseArray[AfterPayApiRequestConfig::TRANSACTION_OUTCOME] ?? null)
87
            ->setCustomer($jsonResponseArray[AfterPayApiRequestConfig::CUSTOMER] ?? [])
88
            ->setCustomerNumber($customerNumber)
89
            ->setPaymentMethods($jsonResponseArray[AfterPayApiRequestConfig::PAYMENT_METHODS] ?? [])
90
            ->setRiskCheckResultCode($riskCheckResultCode)
91
            ->setRiskCheckMessages($this->extractRiskCheckMessages($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\...ractRiskCheckMessages() 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

91
            ->setRiskCheckMessages($this->extractRiskCheckMessages(/** @scrutinizer ignore-type */ $jsonResponseArray));
Loading history...
92
    }
93
94
    /**
95
     * @param array $jsonResponseArray
96
     *
97
     * @return string|null
98
     */
99
    protected function extractRiskCheckCode(array $jsonResponseArray): ?string
100
    {
101
        if (!isset($jsonResponseArray[AfterPayApiRequestConfig::ADDITIONAL_RESPONSE_INFO][AfterPayApiRequestConfig::RISK_CHECK_CODE])) {
102
            return null;
103
        }
104
105
        return $jsonResponseArray[AfterPayApiRequestConfig::ADDITIONAL_RESPONSE_INFO][AfterPayApiRequestConfig::RISK_CHECK_CODE];
106
    }
107
108
    /**
109
     * @param array $jsonResponseArray
110
     *
111
     * @return string|null
112
     */
113
    protected function extractCustomerNumber(array $jsonResponseArray): ?string
114
    {
115
        if (!isset($jsonResponseArray[AfterPayApiRequestConfig::CUSTOMER][AfterPayApiRequestConfig::CUSTOMER_NUMBER])) {
116
            return null;
117
        }
118
119
        return $jsonResponseArray[AfterPayApiRequestConfig::CUSTOMER][AfterPayApiRequestConfig::CUSTOMER_NUMBER];
120
    }
121
122
    /**
123
     * @param array $jsonResponseArray
124
     *
125
     * @return \Generated\Shared\Transfer\AfterPayRiskCheckMessageTransfer[]|\ArrayObject
126
     */
127
    protected function extractRiskCheckMessages(array $jsonResponseArray): ArrayObject
128
    {
129
        $riskCheckMessages = new ArrayObject();
130
131
        if (!isset($jsonResponseArray[AfterPayApiRequestConfig::RISK_CHECK_MESSAGES])) {
132
            return $riskCheckMessages;
133
        }
134
135
        foreach ($jsonResponseArray[AfterPayApiRequestConfig::RISK_CHECK_MESSAGES] as $riskMessage) {
136
            $riskCheckMessages[] = (new AfterPayRiskCheckMessageTransfer())
137
                ->setMessage($riskMessage[AfterPayApiRequestConfig::RISK_CHECK_MESSAGE_MESSAGE])
138
                ->setCustomerFacingMessage($riskMessage[AfterPayApiRequestConfig::RISK_CHECK_MESSAGE_CUSTOMER_FACING_MESSAGE])
139
                ->setType($riskMessage[AfterPayApiRequestConfig::RISK_CHECK_MESSAGE_TYPE])
140
                ->setCode($riskMessage[AfterPayApiRequestConfig::RISK_CHECK_MESSAGE_CODE]);
141
        }
142
143
        return $riskCheckMessages;
144
    }
145
}
146