LookupCustomerCall   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 49
c 1
b 0
f 0
dl 0
loc 127
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 15 2
A buildUserProfileTransfer() 0 20 3
A buildLookupCustomerResponseTransfer() 0 17 3
A __construct() 0 10 1
A buildLookupAddressTransfer() 0 14 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\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\AfterPayToUtilEncodingServiceInterface;
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\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\AfterPayConfig $config
43
     */
44
    public function __construct(
45
        ClientInterface $client,
46
        TransferToCamelCaseArrayConverterInterface $transferConverter,
47
        AfterPayToUtilEncodingServiceInterface $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
            ->setFirstName($userProfile[AfterPayApiRequestConfig::USER_PROFILE_FIRST_NAME])
111
            ->setLastName($userProfile[AfterPayApiRequestConfig::USER_PROFILE_LAST_NAME])
112
            ->setMobileNumber($userProfile[AfterPayApiRequestConfig::USER_PROFILE_MOBILE_NUMBER])
113
            ->setEmail($userProfile[AfterPayApiRequestConfig::USER_PROFILE_EMAIL])
114
            ->setLanguageCode($userProfile[AfterPayApiRequestConfig::USER_PROFILE_LANGUAGE_CODE]);
115
116
        if (!isset($userProfile[AfterPayApiRequestConfig::USER_PROFILE_ADDRESS_LIST])) {
117
            return $userProfileTransfer;
118
        }
119
120
        foreach ($userProfile[AfterPayApiRequestConfig::USER_PROFILE_ADDRESS_LIST] as $userAddress) {
121
            $userProfileTransfer->addLookupAddress(
122
                $this->buildLookupAddressTransfer($userAddress),
123
            );
124
        }
125
126
        return $userProfileTransfer;
127
    }
128
129
    /**
130
     * @param array $userAddress
131
     *
132
     * @return \Generated\Shared\Transfer\AfterPayLookupAddressTransfer
133
     */
134
    protected function buildLookupAddressTransfer(array $userAddress): AfterPayLookupAddressTransfer
135
    {
136
        return (new AfterPayLookupAddressTransfer())
137
            ->setStreet($userAddress[AfterPayApiRequestConfig::USER_PROFILE_ADDRESS_STREET])
138
            ->setStreet2($userAddress[AfterPayApiRequestConfig::USER_PROFILE_ADDRESS_STREET2])
139
            ->setStreet3($userAddress[AfterPayApiRequestConfig::USER_PROFILE_ADDRESS_STREET3])
140
            ->setStreet4($userAddress[AfterPayApiRequestConfig::USER_PROFILE_ADDRESS_STREET4])
141
            ->setStreetNumber($userAddress[AfterPayApiRequestConfig::USER_PROFILE_ADDRESS_STREET_NUMBER])
142
            ->setFlatNo($userAddress[AfterPayApiRequestConfig::USER_PROFILE_ADDRESS_FLAT])
143
            ->setEntrance($userAddress[AfterPayApiRequestConfig::USER_PROFILE_ADDRESS_ENTRANCE])
144
            ->setCity($userAddress[AfterPayApiRequestConfig::USER_PROFILE_ADDRESS_CITY])
145
            ->setPostalCode($userAddress[AfterPayApiRequestConfig::USER_PROFILE_ADDRESS_POSTAL_CODE])
146
            ->setCountry($userAddress[AfterPayApiRequestConfig::USER_PROFILE_ADDRESS_COUNTRY])
147
            ->setCountryCode($userAddress[AfterPayApiRequestConfig::USER_PROFILE_ADDRESS_COUNTRY_CODE]);
148
    }
149
}
150