Passed
Push — master ( c05883...1d5c29 )
by Nikolay
02:30
created

ApiClient::createFileStream()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 3
dl 0
loc 17
ccs 0
cts 15
cp 0
crap 2
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TgBotApi\BotApiBase;
6
7
use Psr\Http\Client\ClientInterface;
8
use Psr\Http\Message\RequestFactoryInterface;
9
use Psr\Http\Message\StreamFactoryInterface;
10
11
class ApiClient implements ApiClientInterface
12
{
13
    private $client;
14
    private $botKey;
15
    private $endPoint;
16
    private $streamFactory;
17
    private $requestFactory;
18
19
    /**
20
     * ApiApiClient constructor.
21
     *
22
     * @param RequestFactoryInterface $requestFactory
23
     * @param StreamFactoryInterface  $streamFactory
24
     * @param ClientInterface         $client
25
     */
26
    public function __construct(
27
        RequestFactoryInterface $requestFactory,
28
        StreamFactoryInterface $streamFactory,
29
        ClientInterface $client
30
    ) {
31
        $this->streamFactory = $streamFactory;
32
        $this->requestFactory = $requestFactory;
33
        $this->client = $client;
34
    }
35
36
    /**
37
     * @param string $method
38
     * @param array  $data
39
     * @param array  $files
40
     *
41
     * @throws \Psr\Http\Client\ClientExceptionInterface
42
     *
43
     * @return mixed
44
     */
45
    public function send(string $method, array $data, array $files = [])
46
    {
47
        $request = $this->requestFactory->createRequest('POST', $this->generateUri($method));
48
49
        $boundary = \uniqid('', true);
50
51
        $stream = $this->streamFactory->createStream($this->createStreamBody($boundary, $data, $files));
52
53
        $response = $this->client->sendRequest($request
54
            ->withHeader('Content-Type', 'multipart/form-data; boundary="' . $boundary . '"')
55
            ->withBody($stream));
56
57
        $content = $response->getBody()->getContents();
58
59
        return \json_decode($content);
60
    }
61
62
    public function setBotKey(string $botKey)
63
    {
64
        $this->botKey = $botKey;
65
    }
66
67
    public function setEndpoint(string $endPoint)
68
    {
69
        $this->endPoint = $endPoint;
70
    }
71
72
    /**
73
     * @param string $method
74
     *
75
     * @return string
76
     */
77
    protected function generateUri(string $method): string
78
    {
79
        return $this->endPoint . '/bot' . $this->botKey . '/' . $method;
80
    }
81
82
    /**
83
     * @param $data
84
     * @param $files
85
     * @param mixed $boundary
86
     *
87
     * @return string
88
     */
89
    protected function createStreamBody($boundary, $data, $files): string
90
    {
91
        $stream = '';
92
        foreach ($data as $name => $value) {
93
            $stream .= $this->createDataStream($boundary, $name, $value);
94
        }
95
96
        foreach ($files as $name => $file) {
97
            $stream .= $this->createFileStream($boundary, $name, $file);
98
        }
99
100
        if (\strlen($stream)) {
101
            $stream .= "--$boundary--\r\n";
102
        }
103
104
        return $stream;
105
    }
106
107
    /**
108
     * @param $boundary
109
     * @param $name
110
     * @param \SplFileInfo $file
111
     *
112
     * @return string
113
     */
114
    protected function createFileStream($boundary, $name, \SplFileInfo $file): string
115
    {
116
        $headers = '';
117
        $headers .= \sprintf(
118
            "Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n",
119
            $name,
120
            $file->getBasename()
121
        );
122
        $headers .= \sprintf("Content-Length: %s\r\n", (string) $file->getSize());
123
        $headers .= \sprintf("Content-Type: %s\r\n", \mime_content_type($file->getRealPath()));
124
125
        $streams = '';
126
        $streams .= "--$boundary\r\n$headers\r\n";
127
        $streams .= \file_get_contents($file->getRealPath());
128
        $streams .= "\r\n";
129
130
        return $streams;
131
    }
132
133
    /**
134
     * @param $boundary
135
     * @param $name
136
     * @param $value
137
     *
138
     * @return string
139
     */
140
    protected function createDataStream($boundary, $name, $value): string
141
    {
142
        $headers = '';
143
        $headers .= \sprintf("Content-Disposition: form-data; name=\"%s\"\r\n", $name);
144
        $headers .= \sprintf("Content-Length: %s\r\n", (string) \strlen((string) $value));
145
146
        $streams = '';
147
        $streams .= "--$boundary\r\n$headers\r\n";
148
        $streams .= $value;
149
        $streams .= "\r\n";
150
151
        return $streams;
152
    }
153
}
154