Completed
Push — feature/eco-574/eco-2266-check... ( b72c03...e5ed68 )
by Ruslan
11s queued 10s
created

extractRiskCheckMessages()   A

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
82
        $responseTransfer = new AfterPayAvailablePaymentMethodsResponseTransfer();
83
84
        $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

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

85
        $customerNumber = $this->extractCustomerNumber(/** @scrutinizer ignore-type */ $jsonResponseArray);
Loading history...
86
87
        $responseTransfer
88
            ->setCheckoutId($jsonResponseArray[AfterPayApiRequestConfig::TRANSACTION_CHECKOUT_ID] ?? null)
89
            ->setOutcome($jsonResponseArray[AfterPayApiRequestConfig::TRANSACTION_OUTCOME] ?? null)
90
            ->setCustomer($jsonResponseArray[AfterPayApiRequestConfig::CUSTOMER] ?? [])
91
            ->setCustomerNumber($customerNumber)
92
            ->setPaymentMethods($jsonResponseArray[AfterPayApiRequestConfig::PAYMENT_METHODS] ?? [])
93
            ->setRiskCheckResultCode($riskCheckResultCode)
94
            ->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

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