Passed
Push — feature/eco-2295/eco-2344-crea... ( 425ba1...d28970 )
by Aleksey
04:38
created

CrefoPayApiClient::performRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 17
nc 2
nop 1
dl 0
loc 27
rs 9.7
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\CrefoPayApi\Business\ApiClient;
9
10
use Generated\Shared\Transfer\CrefoPayApiRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...foPayApiRequestTransfer 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\CrefoPayApiResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...oPayApiResponseTransfer 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\Zed\CrefoPayApi\Business\Logger\CrefoPayApiLoggerInterface;
13
use SprykerEco\Zed\CrefoPayApi\Business\Request\CrefoPayApiRequestInterface;
14
use SprykerEco\Zed\CrefoPayApi\Business\Response\Converter\CrefoPayApiResponseConverterInterface;
15
use SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\CrefoPayApiGuzzleHttpClientAdapterInterface;
16
use SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Exception\CrefoPayApiGuzzleRequestException;
17
18
class CrefoPayApiClient implements CrefoPayApiClientInterface
19
{
20
    /**
21
     * @var \SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\CrefoPayApiGuzzleHttpClientAdapterInterface
22
     */
23
    protected $httpClient;
24
25
    /**
26
     * @var \SprykerEco\Zed\CrefoPayApi\Business\Request\CrefoPayApiRequestInterface
27
     */
28
    protected $request;
29
30
    /**
31
     * @var \SprykerEco\Zed\CrefoPayApi\Business\Response\Converter\CrefoPayApiResponseConverterInterface
32
     */
33
    protected $responseConverter;
34
35
    /**
36
     * @var \SprykerEco\Zed\CrefoPayApi\Business\Logger\CrefoPayApiLoggerInterface
37
     */
38
    protected $logger;
39
40
    /**
41
     * @param \SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\CrefoPayApiGuzzleHttpClientAdapterInterface $httpClient
42
     * @param \SprykerEco\Zed\CrefoPayApi\Business\Request\CrefoPayApiRequestInterface $request
43
     * @param \SprykerEco\Zed\CrefoPayApi\Business\Response\Converter\CrefoPayApiResponseConverterInterface $responseConverter
44
     * @param \SprykerEco\Zed\CrefoPayApi\Business\Logger\CrefoPayApiLoggerInterface $logger
45
     */
46
    public function __construct(
47
        CrefoPayApiGuzzleHttpClientAdapterInterface $httpClient,
48
        CrefoPayApiRequestInterface $request,
49
        CrefoPayApiResponseConverterInterface $responseConverter,
50
        CrefoPayApiLoggerInterface $logger
51
    ) {
52
        $this->httpClient = $httpClient;
53
        $this->request = $request;
54
        $this->responseConverter = $responseConverter;
55
        $this->logger = $logger;
56
    }
57
58
    /**
59
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
60
     *
61
     * @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer
62
     */
63
    public function performRequest(CrefoPayApiRequestTransfer $requestTransfer): CrefoPayApiResponseTransfer
64
    {
65
        $isSuccess = true;
66
67
        try {
68
            $response = $this->httpClient->post(
69
                $this->request->getUrl(),
70
                $this->request->getFormParams($requestTransfer)
71
            );
72
        } catch (CrefoPayApiGuzzleRequestException $requestException) {
73
            $isSuccess = false;
74
            $response = $requestException->getResponse();
75
        }
76
77
        $responseTransfer = $this->responseConverter
78
            ->convertToResponseTransfer($response, $isSuccess);
79
80
        $paymentCrefoPayApiLogTransfer = $this->logger
81
            ->logApiCall(
82
                $requestTransfer,
83
                $responseTransfer,
84
                $this->request->getRequestType()
85
            );
86
87
        $responseTransfer->setCrefoPayApiLogId($paymentCrefoPayApiLogTransfer->getIdPaymentCrefoPayApiLog());
88
89
        return $responseTransfer;
90
    }
91
}
92