SymfonyHttpClient::doRequest()   B
last analyzed

Complexity

Conditions 10
Paths 12

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 26
c 4
b 0
f 0
dl 0
loc 37
rs 7.6666
cc 10
nc 12
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Exception\HttpExceptionInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Contracts\HttpCl...\HttpExceptionInterface 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 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...
10
use TelegramBot\Api\HttpException;
11
12
class SymfonyHttpClient extends AbstractHttpClient
13
{
14
    /**
15
     * @var SymfonyHttpClientInterface
16
     */
17
    private $http;
18
19
    public function __construct(SymfonyHttpClientInterface $http)
20
    {
21
        $this->http = $http;
22
    }
23
24
    /**
25
     * @inheritDoc
26
     */
27
    protected function doRequest($url, array $data = null)
28
    {
29
        $options = [];
30
        if ($data) {
31
            $method = 'POST';
32
33
            $data = array_filter($data);
34
            foreach ($data as &$value) {
35
                if ($value instanceof \CURLFile) {
36
                    $value = DataPart::fromPath($value->getFilename());
37
                } elseif (!\is_string($value) && !\is_array($value)) {
38
                    $value = (string) $value;
39
                }
40
            }
41
            $formData = new FormDataPart($data);
42
43
            $options['headers'] = $formData->getPreparedHeaders()->toArray();
44
            $options['body'] = $formData->toIterable();
45
        } else {
46
            $method = 'GET';
47
        }
48
49
        $response = $this->http->request($method, $url, $options);
50
51
        try {
52
            return $response->toArray();
53
        } catch (ExceptionInterface $exception) {
54
            if ($exception instanceof HttpExceptionInterface) {
55
                $response = $exception->getResponse()->toArray(false);
56
                $message = array_key_exists('description', $response) ? $response['description'] : $exception->getMessage();
57
                $parameters = array_key_exists('parameters', $response) ? $response['parameters'] : [];
58
            } else {
59
                $message = $exception->getMessage();
60
                $parameters = [];
61
            }
62
63
            throw new HttpException($message, $exception->getCode(), $exception, $parameters);
64
        }
65
    }
66
67
    /**
68
     * @inheritDoc
69
     */
70
    protected function doDownload($url)
71
    {
72
        try {
73
            return $this->http->request('GET', $url)->getContent();
74
        } catch (ExceptionInterface $exception) {
75
            throw new HttpException($exception->getMessage(), $exception->getCode(), $exception);
76
        }
77
    }
78
}
79