AbstractAdapter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
eloc 31
c 3
b 0
f 0
dl 0
loc 86
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A sendRequest() 0 12 1
A send() 0 16 2
A __construct() 0 6 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\Inxmail\Business\Api\Adapter;
9
10
use Generated\Shared\Transfer\InxmailRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\InxmailRequestTransfer 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 GuzzleHttp\Client;
12
use GuzzleHttp\Exception\RequestException;
13
use GuzzleHttp\RequestOptions;
14
use Psr\Http\Message\StreamInterface;
15
use SprykerEco\Zed\Inxmail\Business\Exception\InxmailApiHttpRequestException;
16
use SprykerEco\Zed\Inxmail\Dependency\Service\InxmailToUtilEncodingServiceInterface;
17
use SprykerEco\Zed\Inxmail\InxmailConfig;
18
19
abstract class AbstractAdapter implements AdapterInterface
20
{
21
    protected const DEFAULT_TIMEOUT = 45;
22
    protected const DEFAULT_HEADERS = [
23
        'Content-Type' => 'application/json',
24
    ];
25
26
    protected const API_KEY_EVENT = 'event';
27
    protected const API_KEY_PAYLOAD = 'payload';
28
    protected const API_KEY_TRANSACTION_ID = 'transactionId';
29
30
    /**
31
     * @var \GuzzleHttp\Client
32
     */
33
    protected $client;
34
35
    /**
36
     * @var \SprykerEco\Zed\Inxmail\InxmailConfig
37
     */
38
    protected $config;
39
40
    /**
41
     * @var \SprykerEco\Zed\Inxmail\Dependency\Service\InxmailToUtilEncodingServiceInterface
42
     */
43
    protected $utilEncodingService;
44
45
    /**
46
     * @return string
47
     */
48
    abstract protected function getUrl(): string;
49
50
    /**
51
     * @param \SprykerEco\Zed\Inxmail\InxmailConfig $config
52
     * @param \SprykerEco\Zed\Inxmail\Dependency\Service\InxmailToUtilEncodingServiceInterface $utilEncodingService
53
     */
54
    public function __construct(InxmailConfig $config, InxmailToUtilEncodingServiceInterface $utilEncodingService)
55
    {
56
        $this->config = $config;
57
        $this->utilEncodingService = $utilEncodingService;
58
        $this->client = new Client([
59
            RequestOptions::TIMEOUT => static::DEFAULT_TIMEOUT,
60
        ]);
61
    }
62
63
    /**
64
     * @param \Generated\Shared\Transfer\InxmailRequestTransfer $transfer
65
     *
66
     * @return \Psr\Http\Message\StreamInterface
67
     */
68
    public function sendRequest(InxmailRequestTransfer $transfer): StreamInterface
69
    {
70
        $options[RequestOptions::BODY] = $this->utilEncodingService->encodeJson([
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
            static::API_KEY_EVENT => $transfer->getEvent(),
72
            static::API_KEY_TRANSACTION_ID => $transfer->getTransactionId(),
73
            static::API_KEY_PAYLOAD => $transfer->getPayload(),
74
        ]);
75
76
        $options[RequestOptions::HEADERS] = static::DEFAULT_HEADERS;
77
        $options[RequestOptions::AUTH] = [$this->config->getInxmailKeyId(), $this->config->getInxmailSecret()];
78
79
        return $this->send($options);
80
    }
81
82
    /**
83
     * @param array $options
84
     *
85
     * @throws \SprykerEco\Zed\Inxmail\Business\Exception\InxmailApiHttpRequestException
86
     *
87
     * @return \Psr\Http\Message\StreamInterface
88
     */
89
    protected function send(array $options = []): StreamInterface
90
    {
91
        try {
92
            $response = $this->client->post(
93
                $this->getUrl(),
94
                $options
95
            );
96
        } catch (RequestException $requestException) {
97
            throw new InxmailApiHttpRequestException(
98
                $requestException->getMessage(),
99
                $requestException->getCode(),
100
                $requestException
101
            );
102
        }
103
104
        return $response->getBody();
105
    }
106
}
107