Completed
Push — master ( 452aee...d44a9d )
by Alexander
39s queued 14s
created

SymfonyHttpClient::doRequest()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 19
c 3
b 0
f 0
dl 0
loc 28
rs 8.8333
cc 7
nc 4
nop 2
1
<?php
2
3
namespace TelegramBot\Api\Http;
4
5
use Symfony\Component\Mime\Part\DataPart;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Mime\Part\DataPart 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...
6
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Mime\Part\Multipart\FormDataPart 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...
7
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Contracts\HttpCl...tion\ExceptionInterface 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...
8
use Symfony\Contracts\HttpClient\HttpClientInterface as SymfonyHttpClientInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Contracts\HttpClient\HttpClientInterface 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...
9
use TelegramBot\Api\HttpException;
10
11
class SymfonyHttpClient extends AbstractHttpClient
12
{
13
    /**
14
     * @var SymfonyHttpClientInterface
15
     */
16
    private $http;
17
18
    public function __construct(SymfonyHttpClientInterface $http)
19
    {
20
        $this->http = $http;
21
    }
22
23
    /**
24
     * @inheritDoc
25
     */
26
    protected function doRequest($url, array $data = null)
27
    {
28
        $options = [];
29
        if ($data) {
30
            $method = 'POST';
31
32
            $data = array_filter($data);
33
            foreach ($data as &$value) {
34
                if ($value instanceof \CURLFile) {
35
                    $value = DataPart::fromPath($value->getFilename());
36
                } elseif (!\is_string($value) && !\is_array($value)) {
37
                    $value = (string) $value;
38
                }
39
            }
40
            $formData = new FormDataPart($data);
41
42
            $options['headers'] = $formData->getPreparedHeaders()->toArray();
43
            $options['body'] = $formData->toIterable();
44
        } else {
45
            $method = 'GET';
46
        }
47
48
        $response = $this->http->request($method, $url, $options);
49
50
        try {
51
            return $response->toArray();
52
        } catch (ExceptionInterface $exception) {
53
            throw new HttpException($exception->getMessage(), $exception->getCode(), $exception);
54
        }
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60
    protected function doDownload($url)
61
    {
62
        try {
63
            return $this->http->request('GET', $url)->getContent();
64
        } catch (ExceptionInterface $exception) {
65
            throw new HttpException($exception->getMessage(), $exception->getCode(), $exception);
66
        }
67
    }
68
}
69