AbstractAdapter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 69
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A send() 0 5 1
A sendRequest() 0 5 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\AdyenApi\Business\Adapter;
9
10
use GuzzleHttp\Client;
11
use GuzzleHttp\RequestOptions;
12
use Psr\Http\Message\ResponseInterface;
13
use SprykerEco\Zed\AdyenApi\AdyenApiConfig;
14
use SprykerEco\Zed\AdyenApi\Dependency\Service\AdyenApiToUtilEncodingServiceInterface;
15
16
abstract class AbstractAdapter implements AdyenApiAdapterInterface
17
{
18
    protected const DEFAULT_TIMEOUT = 45;
19
    protected const HEADER_CONTENT_TYPE_KEY = 'Content-Type';
20
    protected const HEADER_CONTENT_TYPE_VALUE = 'application/json';
21
    protected const HEADER_X_API_KEY = 'X-API-Key';
22
23
    /**
24
     * @var \SprykerEco\Zed\AdyenApi\AdyenApiConfig
25
     */
26
    protected $config;
27
28
    /**
29
     * @var \SprykerEco\Zed\AdyenApi\Dependency\Service\AdyenApiToUtilEncodingServiceInterface
30
     */
31
    protected $encodingService;
32
33
    /**
34
     * @var \GuzzleHttp\Client
35
     */
36
    protected $client;
37
38
    /**
39
     * @return string
40
     */
41
    abstract protected function getUrl(): string;
42
43
    /**
44
     * @param \SprykerEco\Zed\AdyenApi\AdyenApiConfig $config
45
     * @param \SprykerEco\Zed\AdyenApi\Dependency\Service\AdyenApiToUtilEncodingServiceInterface $encodingService
46
     */
47
    public function __construct(
48
        AdyenApiConfig $config,
49
        AdyenApiToUtilEncodingServiceInterface $encodingService
50
    ) {
51
        $this->client = new Client([
52
            RequestOptions::TIMEOUT => static::DEFAULT_TIMEOUT,
53
            RequestOptions::HEADERS => [
54
                static::HEADER_CONTENT_TYPE_KEY => static::HEADER_CONTENT_TYPE_VALUE,
55
                static::HEADER_X_API_KEY => $config->getApiKey(),
56
            ],
57
        ]);
58
59
        $this->config = $config;
60
        $this->encodingService = $encodingService;
61
    }
62
63
    /**
64
     * @param array $data
65
     *
66
     * @return \Psr\Http\Message\ResponseInterface
67
     */
68
    public function sendRequest(array $data): ResponseInterface
69
    {
70
        $options[RequestOptions::BODY] = $this->encodingService->encodeJson($data);
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...
71
72
        return $this->send($options);
73
    }
74
75
    /**
76
     * @param array $options
77
     *
78
     * @return \Psr\Http\Message\ResponseInterface
79
     */
80
    protected function send(array $options = []): ResponseInterface
81
    {
82
        return $this->client->post(
83
            $this->getUrl(),
84
            $options
85
        );
86
    }
87
}
88