ValidateCustomerCall   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 108
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 15 2
A __construct() 0 12 1
A parseCorrectedAddress() 0 6 1
A extractAddressDataWithUnderscoreKeys() 0 13 3
A buildValidateCustomerResponseTransfer() 0 10 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\Api\Adapter\ApiCall;
9
10
use Generated\Shared\Transfer\AfterPayRequestAddressTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...yRequestAddressTransfer 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\AfterPayValidateCustomerRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...CustomerRequestTransfer 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\AfterPayValidateCustomerResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ustomerResponseTransfer 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 SprykerEco\Shared\AfterPay\AfterPayApiRequestConfig;
14
use SprykerEco\Zed\AfterPay\AfterPayConfig;
15
use SprykerEco\Zed\AfterPay\Business\Api\Adapter\Client\ClientInterface;
16
use SprykerEco\Zed\AfterPay\Business\Api\Adapter\Converter\TransferToCamelCaseArrayConverterInterface;
17
use SprykerEco\Zed\AfterPay\Business\Exception\ApiHttpRequestException;
18
use SprykerEco\Zed\AfterPay\Dependency\Service\AfterPayToUtilEncodingServiceInterface;
19
use SprykerEco\Zed\AfterPay\Dependency\Service\AfterPayToUtilTextServiceInterface;
20
21
class ValidateCustomerCall extends AbstractApiCall implements ValidateCustomerCallInterface
22
{
23
    /**
24
     * @var \SprykerEco\Zed\AfterPay\Business\Api\Adapter\Client\ClientInterface
25
     */
26
    protected $client;
27
28
    /**
29
     * @var \SprykerEco\Zed\AfterPay\Dependency\Service\AfterPayToUtilTextServiceInterface
30
     */
31
    protected $utilText;
32
33
    /**
34
     * @var \SprykerEco\Zed\AfterPay\AfterPayConfig
35
     */
36
    protected $config;
37
38
    /**
39
     * @param \SprykerEco\Zed\AfterPay\Business\Api\Adapter\Client\ClientInterface $client
40
     * @param \SprykerEco\Zed\AfterPay\Business\Api\Adapter\Converter\TransferToCamelCaseArrayConverterInterface $transferConverter
41
     * @param \SprykerEco\Zed\AfterPay\Dependency\Service\AfterPayToUtilEncodingServiceInterface $utilEncoding
42
     * @param \SprykerEco\Zed\AfterPay\Dependency\Service\AfterPayToUtilTextServiceInterface $utilText
43
     * @param \SprykerEco\Zed\AfterPay\AfterPayConfig $config
44
     */
45
    public function __construct(
46
        ClientInterface $client,
47
        TransferToCamelCaseArrayConverterInterface $transferConverter,
48
        AfterPayToUtilEncodingServiceInterface $utilEncoding,
49
        AfterPayToUtilTextServiceInterface $utilText,
50
        AfterPayConfig $config
51
    ) {
52
        $this->client = $client;
53
        $this->utilEncoding = $utilEncoding;
54
        $this->utilText = $utilText;
55
        $this->config = $config;
56
        $this->transferConverter = $transferConverter;
57
    }
58
59
    /**
60
     * @param \Generated\Shared\Transfer\AfterPayValidateCustomerRequestTransfer $validateCustomerRequestTransfer
61
     *
62
     * @return \Generated\Shared\Transfer\AfterPayValidateCustomerResponseTransfer
63
     */
64
    public function execute(AfterPayValidateCustomerRequestTransfer $validateCustomerRequestTransfer): AfterPayValidateCustomerResponseTransfer
65
    {
66
        $jsonRequest = $this->buildJsonRequestFromTransferObject($validateCustomerRequestTransfer);
67
68
        try {
69
            $jsonResponse = $this->client->sendPost(
70
                $this->config->getValidateAddressApiEndpointUrl(),
71
                $jsonRequest,
72
            );
73
        } catch (ApiHttpRequestException $apiHttpRequestException) {
74
            $this->logApiException($apiHttpRequestException);
75
            $jsonResponse = '[]';
76
        }
77
78
        return $this->buildValidateCustomerResponseTransfer($jsonResponse);
79
    }
80
81
    /**
82
     * @param string $jsonResponse
83
     *
84
     * @return \Generated\Shared\Transfer\AfterPayValidateCustomerResponseTransfer
85
     */
86
    protected function buildValidateCustomerResponseTransfer(string $jsonResponse): AfterPayValidateCustomerResponseTransfer
87
    {
88
        $jsonResponseArray = $this->utilEncoding->decodeJson($jsonResponse, true);
89
90
        return (new AfterPayValidateCustomerResponseTransfer())
91
            ->setIsValid($jsonResponseArray[AfterPayApiRequestConfig::VALIDATE_ADDRESS_IS_VALID] ?? false)
92
            ->setCorrectedAddress(
93
                $this->parseCorrectedAddress($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\...parseCorrectedAddress() 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

93
                $this->parseCorrectedAddress(/** @scrutinizer ignore-type */ $jsonResponseArray),
Loading history...
94
            )
95
            ->setResponsePayload($jsonResponse);
96
    }
97
98
    /**
99
     * @param array $jsonResponseArray
100
     *
101
     * @return \Generated\Shared\Transfer\AfterPayRequestAddressTransfer
102
     */
103
    protected function parseCorrectedAddress(array $jsonResponseArray): AfterPayRequestAddressTransfer
104
    {
105
        $correctedAddressArray = $this->extractAddressDataWithUnderscoreKeys($jsonResponseArray);
106
107
        return (new AfterPayRequestAddressTransfer())
108
            ->fromArray($correctedAddressArray, true);
109
    }
110
111
    /**
112
     * @param array $jsonResponseArray
113
     *
114
     * @return array
115
     */
116
    protected function extractAddressDataWithUnderscoreKeys(array $jsonResponseArray): array
117
    {
118
        if (!isset($jsonResponseArray[AfterPayApiRequestConfig::CORRECTED_ADDRESS])) {
119
            return [];
120
        }
121
122
        $addressWithUnderscoreKeys = [];
123
        foreach ($jsonResponseArray[AfterPayApiRequestConfig::CORRECTED_ADDRESS] as $key => $value) {
124
            $keyWithUnderscore = $this->utilText->camelCaseToSeparator($key, '_');
125
            $addressWithUnderscoreKeys[$keyWithUnderscore] = $value;
126
        }
127
128
        return $addressWithUnderscoreKeys;
129
    }
130
}
131