AbstractAdapter::send()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 16
rs 9.9332
cc 2
nc 3
nop 3
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\Easycredit\Business\Api\Adapter\Http;
9
10
use Generated\Shared\Transfer\EasycreditRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...sycreditRequestTransfer 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\EasycreditResponseErrorTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...itResponseErrorTransfer 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\EasycreditResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ycreditResponseTransfer 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 GuzzleHttp\ClientInterface;
14
use GuzzleHttp\Exception\RequestException;
15
use GuzzleHttp\RequestOptions;
16
use SprykerEco\Service\Easycredit\Dependency\Service\EasycreditToUtilEncodingServiceInterface;
17
use SprykerEco\Zed\Easycredit\Business\Api\Adapter\EasycreditAdapterInterface;
18
use SprykerEco\Zed\Easycredit\EasycreditConfig;
19
20
abstract class AbstractAdapter implements EasycreditAdapterInterface
21
{
22
    protected const API_KEY_EVENT = 'event';
23
    protected const API_KEY_PAYLOAD = 'payload';
24
    protected const API_KEY_TRANSACTION_ID = 'transactionId';
25
26
    protected const HEADER_TBK_RK_SHOP = 'tbk-rk-shop';
27
    protected const HEADER_TBK_RK_TOKEN = 'tbk-rk-token';
28
    protected const HEADER_CONTENT_TYPE = 'Content-Type';
29
    protected const CONTENT_TYPE_JSON = 'application/json';
30
31
    protected const REQUEST_TYPE_TEXT = 'texte';
32
    protected const REQUEST_TYPE_PROCESS = 'vorgang';
33
34
    /**
35
     * @var \GuzzleHttp\ClientInterface
36
     */
37
    protected $httpClient;
38
39
    /**
40
     * @var \SprykerEco\Service\Easycredit\Dependency\Service\EasycreditToUtilEncodingServiceInterface
41
     */
42
    protected $utilEncodingService;
43
44
    /**
45
     * @var \SprykerEco\Zed\Easycredit\EasycreditConfig
46
     */
47
    protected $config;
48
49
    /**
50
     * @param \Generated\Shared\Transfer\EasycreditRequestTransfer $easycreditRequestTransfer
51
     *
52
     * @return string
53
     */
54
    abstract protected function getUrl(EasycreditRequestTransfer $easycreditRequestTransfer): string;
55
56
    /**
57
     * @return string
58
     */
59
    abstract protected function getMethod(): string;
60
61
    /**
62
     * @param \GuzzleHttp\ClientInterface $client
63
     * @param \SprykerEco\Service\Easycredit\Dependency\Service\EasycreditToUtilEncodingServiceInterface $utilEncodingService
64
     * @param \SprykerEco\Zed\Easycredit\EasycreditConfig $config
65
     */
66
    public function __construct(
67
        ClientInterface $client,
68
        EasycreditToUtilEncodingServiceInterface $utilEncodingService,
69
        EasycreditConfig $config
70
    ) {
71
        $this->httpClient = $client;
72
        $this->utilEncodingService = $utilEncodingService;
73
        $this->config = $config;
74
    }
75
76
    /**
77
     * @param \Generated\Shared\Transfer\EasycreditRequestTransfer $easycreditRequestTransfer
78
     *
79
     * @return \Generated\Shared\Transfer\EasycreditResponseTransfer
80
     */
81
    public function sendRequest(EasycreditRequestTransfer $easycreditRequestTransfer): EasycreditResponseTransfer
82
    {
83
        $url = $this->getUrl($easycreditRequestTransfer);
84
        $method = $this->getMethod();
85
        $options[RequestOptions::BODY] = $this->utilEncodingService->encodeJson($easycreditRequestTransfer->getPayload());
0 ignored issues
show
Comprehensibility Best Practice introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.
Loading history...
86
        $options[RequestOptions::HEADERS] = $this->getHeaders();
87
88
        return $this->send($method, $url, $options);
89
    }
90
91
    /**
92
     * @param string $method
93
     * @param string $url
94
     * @param array $options
95
     *
96
     * @return \Generated\Shared\Transfer\EasycreditResponseTransfer
97
     */
98
    protected function send(string $method, string $url, array $options = []): EasycreditResponseTransfer
99
    {
100
        $responseTransfer = new EasycreditResponseTransfer();
101
102
        try {
103
            $response = $this->httpClient->request($method, $url, $options);
104
            $responseTransfer->setBody($this->utilEncodingService->decodeJson($response->getBody(), true));
105
        } catch (RequestException $requestException) {
106
            $errorTransfer = new EasycreditResponseErrorTransfer();
107
            $errorTransfer->setErrorCode($requestException->getCode());
108
            $errorTransfer->setErrorMessage($requestException->getMessage());
109
110
            $responseTransfer->setError($errorTransfer);
111
        }
112
113
        return $responseTransfer;
114
    }
115
116
    /**
117
     * @return array
118
     */
119
    protected function getHeaders(): array
120
    {
121
        return [
122
            static::HEADER_CONTENT_TYPE => static::CONTENT_TYPE_JSON,
123
            static::HEADER_TBK_RK_SHOP => $this->config->getShopIdentifier(),
124
            static::HEADER_TBK_RK_TOKEN => $this->config->getShopToken(),
125
        ];
126
    }
127
}
128