Passed
Pull Request — feature/eco-2295/dev (#1)
by Aleksey
02:44 queued 01:11
created

CrefoPayApiGuzzleHttpClientAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
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\Dependency\External\Guzzle;
9
10
use GuzzleHttp\Client;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Client 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\Exception\RequestException;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Exception\RequestException 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 GuzzleHttp\RequestOptions;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\RequestOptions 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 SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Exception\CrefoPayApiGuzzleRequestException;
14
use SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponse;
15
use SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface;
16
17
class CrefoPayApiGuzzleHttpClientAdapter implements CrefoPayApiGuzzleHttpClientAdapterInterface
18
{
19
    protected const DEFAULT_TIMEOUT = 45;
20
    protected const HEADER_CONTENT_TYPE_KEY = 'Content-Type';
21
    protected const HEADER_CONTENT_TYPE_VALUE = 'application/x-www-form-urlencoded';
22
23
    /**
24
     * @var \GuzzleHttp\Client
25
     */
26
    protected $guzzleHttpClient;
27
28
    public function __construct()
29
    {
30
        $this->guzzleHttpClient = new Client([
31
            RequestOptions::TIMEOUT => static::DEFAULT_TIMEOUT,
32
            RequestOptions::HEADERS => [
33
                static::HEADER_CONTENT_TYPE_KEY => static::HEADER_CONTENT_TYPE_VALUE,
34
            ],
35
        ]);
36
    }
37
38
    /**
39
     * @param string $url
40
     * @param array $formParams
41
     *
42
     * @throws \SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Exception\CrefoPayApiGuzzleRequestException
43
     *
44
     * @return \SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface
45
     */
46
    public function post(string $url, array $formParams = []): CrefoPayApiGuzzleResponseInterface
47
    {
48
        try {
49
            $options = [
50
                RequestOptions::FORM_PARAMS => $formParams,
51
            ];
52
            $response = $this->guzzleHttpClient->post($url, $options);
53
        } catch (RequestException $requestException) {
54
            $response = new CrefoPayApiGuzzleResponse(
55
                $requestException->getResponse()->getBody(),
56
                $requestException->getResponse()->getHeaders()
57
            );
58
            throw new CrefoPayApiGuzzleRequestException(
59
                $response,
60
                $requestException->getMessage(),
61
                $requestException->getCode(),
62
                $requestException
63
            );
64
        }
65
66
        return new CrefoPayApiGuzzleResponse(
67
            $response->getBody(),
68
            $response->getHeaders()
69
        );
70
    }
71
}
72