Passed
Pull Request — master (#18)
by Volodymyr
11:32 queued 03:05
created

AuthorizePaymentCall::findCustomerNumber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
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\AfterPayApiResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...rPayApiResponseTransfer 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\AfterPayAuthorizeRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...uthorizeRequestTransfer 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 SprykerEco\Shared\AfterPay\AfterPayApiRequestConfig;
13
use SprykerEco\Shared\AfterPay\AfterPayConfig as SharedAfterPayConfig;
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
20
class AuthorizePaymentCall extends AbstractApiCall implements AuthorizePaymentCallInterface
21
{
22
    /**
23
     * @var \SprykerEco\Zed\AfterPay\Business\Api\Adapter\Client\ClientInterface
24
     */
25
    protected $client;
26
27
    /**
28
     * @var \SprykerEco\Zed\AfterPay\AfterPayConfig
29
     */
30
    protected $config;
31
32
    /**
33
     * @param \SprykerEco\Zed\AfterPay\Business\Api\Adapter\Client\ClientInterface $client
34
     * @param \SprykerEco\Zed\AfterPay\Business\Api\Adapter\Converter\TransferToCamelCaseArrayConverterInterface $transferConverter
35
     * @param \SprykerEco\Zed\AfterPay\Dependency\Service\AfterPayToUtilEncodingServiceInterface $utilEncoding
36
     * @param \SprykerEco\Zed\AfterPay\AfterPayConfig $config
37
     */
38
    public function __construct(
39
        ClientInterface $client,
40
        TransferToCamelCaseArrayConverterInterface $transferConverter,
41
        AfterPayToUtilEncodingServiceInterface $utilEncoding,
42
        AfterPayConfig $config
43
    ) {
44
        $this->client = $client;
45
        $this->transferConverter = $transferConverter;
46
        $this->utilEncoding = $utilEncoding;
47
        $this->config = $config;
48
    }
49
50
    /**
51
     * @param \Generated\Shared\Transfer\AfterPayAuthorizeRequestTransfer $requestTransfer
52
     *
53
     * @return \Generated\Shared\Transfer\AfterPayApiResponseTransfer
54
     */
55
    public function execute(AfterPayAuthorizeRequestTransfer $requestTransfer): AfterPayApiResponseTransfer
56
    {
57
        $jsonRequest = $this->buildJsonRequestFromTransferObject($requestTransfer);
58
59
        try {
60
            $jsonResponse = $this->client->sendPost(
61
                $this->config->getAuthorizeApiEndpointUrl(),
62
                $jsonRequest
63
            );
64
        } catch (ApiHttpRequestException $apiHttpRequestException) {
65
            $this->logApiException($apiHttpRequestException);
66
            $jsonResponse = '[]';
67
        }
68
69
        return $this->buildAuthorizeResponseTransfer($jsonResponse);
70
    }
71
72
    /**
73
     * @param string $jsonResponse
74
     *
75
     * @return \Generated\Shared\Transfer\AfterPayApiResponseTransfer
76
     */
77
    protected function buildAuthorizeResponseTransfer(string $jsonResponse): AfterPayApiResponseTransfer
78
    {
79
        $jsonResponseArray = $this->utilEncoding->decodeJson($jsonResponse, true);
80
81
        return (new AfterPayApiResponseTransfer())
82
            ->setOutcome($jsonResponseArray[AfterPayApiRequestConfig::TRANSACTION_OUTCOME] ?? SharedAfterPayConfig::API_TRANSACTION_OUTCOME_REJECTED)
83
            ->setReservationId($jsonResponseArray[AfterPayApiRequestConfig::TRANSACTION_RESERVATION_ID] ?? null)
84
            ->setCheckoutId($jsonResponseArray[AfterPayApiRequestConfig::TRANSACTION_CHECKOUT_ID] ?? null)
85
            ->setCustomerNumber($this->findCustomerNumber($jsonResponseArray))
0 ignored issues
show
Bug introduced by
It seems like $jsonResponseArray can also be of type null; however, parameter $response of SprykerEco\Zed\AfterPay\...l::findCustomerNumber() 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
            ->setCustomerNumber($this->findCustomerNumber(/** @scrutinizer ignore-type */ $jsonResponseArray))
Loading history...
86
            ->setResponsePayload($jsonResponse);
87
    }
88
89
    /**
90
     * @param array $response
91
     *
92
     * @return string|null
93
     */
94
    protected function findCustomerNumber(array $response): ?string
95
    {
96
        $customerData = $response[AfterPayApiRequestConfig::CUSTOMER] ?? [];
97
98
        return $customerData[AfterPayApiRequestConfig::CUSTOMER_NUMBER] ?? null;
99
    }
100
}
101