Passed
Pull Request — feature/eco-2295/dev (#1)
by Aleksey
03:39 queued 01:40
created

CrefoPayApiGuzzleHttpClientAdapter::post()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 2
dl 0
loc 18
rs 9.8666
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($requestException->getResponse()->getBody());
55
            throw new CrefoPayApiGuzzleRequestException(
56
                $response,
57
                $requestException->getMessage(),
58
                $requestException->getCode(),
59
                $requestException
60
            );
61
        }
62
63
        return new CrefoPayApiGuzzleResponse($response->getBody());
64
    }
65
}
66