FactFinderAbstractAdapter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 109
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 3 1
A sendRequest() 0 7 1
A __construct() 0 8 1
A getHeaders() 0 4 1
A getOptions() 0 7 1
A getAuth() 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\Client\FactFinderNg\Api\Adapter\Http;
9
10
use Generated\Shared\Transfer\FactFinderNgRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...FinderNgRequestTransfer 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\ClientInterface;
12
use GuzzleHttp\RequestOptions;
13
use Psr\Http\Message\ResponseInterface;
14
use SprykerEco\Client\FactFinderNg\Api\Adapter\FactFinderNgAdapterInterface;
15
use SprykerEco\Client\FactFinderNg\Dependency\Service\FactFinderNgToUtilEncodingServiceInterface;
16
use SprykerEco\Client\FactFinderNg\FactFinderNgConfig;
17
18
abstract class FactFinderAbstractAdapter implements FactFinderNgAdapterInterface
19
{
20
    protected const HEADER_KEY_CONTENT_TYPE = 'Content-Type';
21
    protected const HEADER_VALUE_APPLICATION_JSON = 'application/json';
22
23
    protected const FACT_FINDER_URL_TYPE_URL = 'rest';
24
    protected const FACT_FINDER_URL_VERSION = 'v2';
25
    protected const FACT_FINDER_URL_TRACK = 'track';
26
27
    /**
28
     * @var \GuzzleHttp\ClientInterface
29
     */
30
    protected $httpClient;
31
32
    /**
33
     * @var \SprykerEco\Client\FactFinderNg\Dependency\Service\FactFinderNgToUtilEncodingServiceInterface
34
     */
35
    protected $utilEncodingService;
36
37
    /**
38
     * @var \SprykerEco\Client\FactFinderNg\FactFinderNgConfig
39
     */
40
    protected $config;
41
42
    /**
43
     * @param \Generated\Shared\Transfer\FactFinderNgRequestTransfer $factFinderNgRequestTransfer
44
     *
45
     * @return string
46
     */
47
    abstract protected function getUrl(FactFinderNgRequestTransfer $factFinderNgRequestTransfer): string;
48
49
    /**
50
     * @return string
51
     */
52
    abstract protected function getMethod(): string;
53
54
    /**
55
     * @param \GuzzleHttp\ClientInterface $client
56
     * @param \SprykerEco\Client\FactFinderNg\Dependency\Service\FactFinderNgToUtilEncodingServiceInterface $utilEncodingService
57
     * @param \SprykerEco\Client\FactFinderNg\FactFinderNgConfig $config
58
     */
59
    public function __construct(
60
        ClientInterface $client,
61
        FactFinderNgToUtilEncodingServiceInterface $utilEncodingService,
62
        FactFinderNgConfig $config
63
    ) {
64
        $this->httpClient = $client;
65
        $this->utilEncodingService = $utilEncodingService;
66
        $this->config = $config;
67
    }
68
69
    /**
70
     * @param \Generated\Shared\Transfer\FactFinderNgRequestTransfer $factFinderNgRequestTransfer
71
     *
72
     * @return \Psr\Http\Message\ResponseInterface
73
     */
74
    public function sendRequest(FactFinderNgRequestTransfer $factFinderNgRequestTransfer): ResponseInterface
75
    {
76
        $url = $this->getUrl($factFinderNgRequestTransfer);
77
        $method = $this->getMethod();
78
        $options = $this->getOptions($factFinderNgRequestTransfer);
79
80
        return $this->send($method, $url, $options);
81
    }
82
83
    /**
84
     * @param string $method
85
     * @param string $url
86
     * @param array $options
87
     *
88
     * @return \Psr\Http\Message\ResponseInterface
89
     */
90
    protected function send(string $method, string $url, array $options = []): ResponseInterface
91
    {
92
        return $this->httpClient->request($method, $url, $options);
93
    }
94
95
    /**
96
     * @param \Generated\Shared\Transfer\FactFinderNgRequestTransfer $factFinderNgRequestTransfer
97
     *
98
     * @return array
99
     */
100
    protected function getOptions(FactFinderNgRequestTransfer $factFinderNgRequestTransfer): array
101
    {
102
        $options[RequestOptions::BODY] = $this->utilEncodingService->encodeJson($factFinderNgRequestTransfer->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...
103
        $options[RequestOptions::HEADERS] = $this->getHeaders();
104
        $options[RequestOptions::AUTH] = $this->getAuth();
105
106
        return $options;
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    protected function getHeaders(): array
113
    {
114
        return [
115
            static::HEADER_KEY_CONTENT_TYPE => static::HEADER_VALUE_APPLICATION_JSON,
116
        ];
117
    }
118
119
    /**
120
     * @return array
121
     */
122
    protected function getAuth(): array
123
    {
124
        return [
125
            $this->config->getFactFinderUsername(),
126
            $this->config->getFactFinderPassword(),
127
        ];
128
    }
129
}
130