Test Failed
Push — draft ( 48d64f...f72ceb )
by Maxim
02:19
created

HttpClient::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Greenplugin\TelegramBot;
6
7
use Greenplugin\TelegramBot\Type\InputFileType;
8
use Nyholm\Psr7\Request;
9
use Psr\Http\Client\ClientInterface;
10
11
class HttpClient implements HttpClientInterface
12
{
13
    private $client;
14
15
    public function __construct(ClientInterface $client)
16
    {
17
        $this->client = $client;
18
    }
19
20
    public function get(string $path)
21
    {
22
        $request = new Request('GET', $path);
23
24
        $response = $this->client->sendRequest($request);
25
26
        return \json_decode($response->getBody()->getContents());
27
    }
28
29
    public function post(string $path, array $data)
30
    {
31
        $request = new Request('POST', $path, ['Content-Type' => 'application/json'], \json_encode($data));
32
33
        $response = $this->client->sendRequest($request);
34
35
        return \json_decode($response->getBody()->getContents());
36
    }
37
38
    public function form(string $path, array $data)
39
    {
40
        $boundary = uniqid('', true);
41
        $headers = [];
42
        $headers['Content-Type'] = 'multipart/form-data; boundary="'.$boundary.'"';
43
        $body = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $body is dead and can be removed.
Loading history...
44
        $files = '';
45
        foreach ($data as $name => $value) {
46
            if (isset($value['path'])) {
47
                $fileContent = file_get_contents($value['path']);
48
                $files .= $this->prepareMultipart($name, $fileContent, $boundary, $value);
49
            } else {
50
                $files .= $this->prepareMultipart($name, (string)$value, $boundary);
51
            }
52
        }
53
54
        $body = "$files--{$boundary}--\r\n";
55
56
        $request = new Request('POST', $path, $headers, $body);
57
58
        $response = $this->client->sendRequest($request);
59
60
        $content = $response->getBody()->getContents();
61
62
        return \json_decode($content);
63
    }
64
65
    private function prepareMultipart(string $name, string $content, string $boundary, array $data = []): string
66
    {
67
        $output = '';
68
        $fileHeaders = [];
69
        // Set a default content-disposition header
70
        $fileHeaders['Content-Disposition'] = sprintf('form-data; name="%s"', $name);
71
        if (isset($data['filename'])) {
72
            $fileHeaders['Content-Disposition'] .= sprintf('; filename="%s"', $data['filename']);
73
        }
74
        // Set a default content-length header
75
        if ($length = \strlen($content)) {
76
            $fileHeaders['Content-Length'] = (string) $length;
77
        }
78
        if (isset($data['contentType'])) {
79
            $fileHeaders['Content-Type'] = $data['contentType'];
80
        }
81
        // Add start
82
        $output .= "--$boundary\r\n";
83
        foreach ($fileHeaders as $key => $value) {
84
            $output .= sprintf("%s: %s\r\n", $key, $value);
85
        }
86
        $output .= "\r\n";
87
        $output .= $content;
88
        $output .= "\r\n";
89
        return $output;
90
    }
91
92
}
93