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

LookupCustomerCall::buildLookupAddressTransfer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 1
dl 0
loc 18
rs 9.7998
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\AfterpayCustomerLookupRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...erLookupRequestTransfer 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\AfterpayCustomerLookupResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...rLookupResponseTransfer 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\AfterpayLookupAddressTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ayLookupAddressTransfer 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\AfterpayUserProfileTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...rpayUserProfileTransfer 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\AfterpayToUtilEncodingInterface;
20
21
class LookupCustomerCall extends AbstractApiCall implements LookupCustomerCallInterface
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\AfterpayToUtilTextInterface
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\AfterpayToUtilEncodingInterface $utilEncoding
42
     * @param \SprykerEco\Zed\Afterpay\AfterpayConfig $config
43
     */
44
    public function __construct(
45
        ClientInterface $client,
46
        TransferToCamelCaseArrayConverterInterface $transferConverter,
47
        AfterpayToUtilEncodingInterface $utilEncoding,
48
        AfterpayConfig $config
49
    ) {
50
        $this->client = $client;
51
        $this->utilEncoding = $utilEncoding;
52
        $this->config = $config;
53
        $this->transferConverter = $transferConverter;
54
    }
55
56
    /**
57
     * @param \Generated\Shared\Transfer\AfterpayCustomerLookupRequestTransfer $customerLookupRequestTransfer
58
     *
59
     * @return \Generated\Shared\Transfer\AfterpayCustomerLookupResponseTransfer
60
     */
61
    public function execute(AfterpayCustomerLookupRequestTransfer $customerLookupRequestTransfer): AfterpayCustomerLookupResponseTransfer
62
    {
63
        $jsonRequest = $this->buildJsonRequestFromTransferObject($customerLookupRequestTransfer);
64
65
        try {
66
            $jsonResponse = $this->client->sendPost(
67
                $this->config->getLookupCustomerApiEndpointUrl(),
68
                $jsonRequest
69
            );
70
        } catch (ApiHttpRequestException $apiHttpRequestException) {
71
            $this->logApiException($apiHttpRequestException);
72
            $jsonResponse = '[]';
73
        }
74
75
        return $this->buildLookupCustomerResponseTransfer($jsonResponse);
76
    }
77
78
    /**
79
     * @param string $jsonResponse
80
     *
81
     * @return \Generated\Shared\Transfer\AfterpayCustomerLookupResponseTransfer
82
     */
83
    protected function buildLookupCustomerResponseTransfer(string $jsonResponse): AfterpayCustomerLookupResponseTransfer
84
    {
85
        $jsonResponseArray = $this->utilEncoding->decodeJson($jsonResponse, true);
86
87
        $responseTransfer = new AfterpayCustomerLookupResponseTransfer();
88
89
        if (!isset($jsonResponseArray[AfterpayApiRequestConfig::USER_PROFILES])) {
90
            return $responseTransfer;
91
        }
92
93
        foreach ($jsonResponseArray[AfterpayApiRequestConfig::USER_PROFILES] as $userProfile) {
94
            $responseTransfer->addUserProfile(
95
                $this->buildUserProfileTransfer($userProfile)
96
            );
97
        }
98
99
        return $responseTransfer;
100
    }
101
102
    /**
103
     * @param array $userProfile
104
     *
105
     * @return \Generated\Shared\Transfer\AfterpayUserProfileTransfer
106
     */
107
    protected function buildUserProfileTransfer(array $userProfile): AfterpayUserProfileTransfer
108
    {
109
        $userProfileTransfer = new AfterpayUserProfileTransfer();
110
111
        $userProfileTransfer
112
            ->setFirstName($userProfile[AfterpayApiRequestConfig::USER_PROFILE_FIRST_NAME])
113
            ->setLastName($userProfile[AfterpayApiRequestConfig::USER_PROFILE_LAST_NAME])
114
            ->setMobileNumber($userProfile[AfterpayApiRequestConfig::USER_PROFILE_MOBILE_NUMBER])
115
            ->setEmail($userProfile[AfterpayApiRequestConfig::USER_PROFILE_EMAIL])
116
            ->setLanguageCode($userProfile[AfterpayApiRequestConfig::USER_PROFILE_LANGUAGE_CODE]);
117
118
        if (!isset($userProfile[AfterpayApiRequestConfig::USER_PROFILE_ADDRESS_LIST])) {
119
            return $userProfileTransfer;
120
        }
121
122
        foreach ($userProfile[AfterpayApiRequestConfig::USER_PROFILE_ADDRESS_LIST] as $userAddress) {
123
            $userProfileTransfer->addLookupAddress(
124
                $this->buildLookupAddressTransfer($userAddress)
125
            );
126
        }
127
128
        return $userProfileTransfer;
129
    }
130
131
    /**
132
     * @param array $userAddress
133
     *
134
     * @return \Generated\Shared\Transfer\AfterpayLookupAddressTransfer
135
     */
136
    protected function buildLookupAddressTransfer(array $userAddress): AfterpayLookupAddressTransfer
137
    {
138
        $lookupAddressTransfer = new AfterpayLookupAddressTransfer();
139
140
        $lookupAddressTransfer
141
            ->setStreet($userAddress[AfterpayApiRequestConfig::USER_PROFILE_ADDRESS_STREET])
142
            ->setStreet2($userAddress[AfterpayApiRequestConfig::USER_PROFILE_ADDRESS_STREET2])
143
            ->setStreet3($userAddress[AfterpayApiRequestConfig::USER_PROFILE_ADDRESS_STREET3])
144
            ->setStreet4($userAddress[AfterpayApiRequestConfig::USER_PROFILE_ADDRESS_STREET4])
145
            ->setStreetNumber($userAddress[AfterpayApiRequestConfig::USER_PROFILE_ADDRESS_STREET_NUMBER])
146
            ->setFlatNo($userAddress[AfterpayApiRequestConfig::USER_PROFILE_ADDRESS_FLAT])
147
            ->setEntrance($userAddress[AfterpayApiRequestConfig::USER_PROFILE_ADDRESS_ENTRANCE])
148
            ->setCity($userAddress[AfterpayApiRequestConfig::USER_PROFILE_ADDRESS_CITY])
149
            ->setPostalCode($userAddress[AfterpayApiRequestConfig::USER_PROFILE_ADDRESS_POSTAL_CODE])
150
            ->setCountry($userAddress[AfterpayApiRequestConfig::USER_PROFILE_ADDRESS_COUNTRY])
151
            ->setCountryCode($userAddress[AfterpayApiRequestConfig::USER_PROFILE_ADDRESS_COUNTRY_CODE]);
152
153
        return $lookupAddressTransfer;
154
    }
155
}
156