Completed
Push — master ( 588078...30ae0c )
by Nikolay
04:51 queued 02:15
created

ApiClient::generateUri()   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 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\ClientException
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
        $stream .= "--$boundary--\r\n";
101
102
        return $stream;
103
    }
104
105
    /**
106
     * @param $boundary
107
     * @param $name
108
     * @param \SplFileInfo $file
109
     *
110
     * @return string
111
     */
112
    protected function createFileStream($boundary, $name, \SplFileInfo $file): string
113
    {
114
        $headers = '';
115
        $headers .= \sprintf(
116
            "Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n",
117
            $name,
118
            $file->getBasename()
119
        );
120
        $headers .= \sprintf("Content-Length: %s\r\n", (string) $file->getSize());
121
        $headers .= \sprintf("Content-Type: %s\r\n", \mime_content_type($file->getRealPath()));
122
123
        $streams = '';
124
        $streams .= "--$boundary\r\n$headers\r\n";
125
        $streams .= \file_get_contents($file->getRealPath());
126
        $streams .= "\r\n";
127
128
        return $streams;
129
    }
130
131
    /**
132
     * @param $boundary
133
     * @param $name
134
     * @param $value
135
     *
136
     * @return string
137
     */
138
    protected function createDataStream($boundary, $name, $value): string
139
    {
140
        $headers = '';
141
        $headers .= \sprintf("Content-Disposition: form-data; name=\"%s\"\r\n", $name);
142
        $headers .= \sprintf("Content-Length: %s\r\n", (string) \strlen((string) $value));
143
144
        $streams = '';
145
        $streams .= "--$boundary\r\n$headers\r\n";
146
        $streams .= $value;
147
        $streams .= "\r\n";
148
149
        return $streams;
150
    }
151
}
152