Passed
Push — draft ( 541bbe...117fb3 )
by Maxim
02:29
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(RequestFactoryInterface $requestFactory, StreamFactoryInterface $streamFactory, ClientInterface $client)
27
    {
28
        $this->streamFactory = $streamFactory;
29
        $this->requestFactory = $requestFactory;
30
        $this->client = $client;
31
    }
32
33
    /**
34
     * @param string $method
35
     * @param array  $data
36
     * @param array  $files
37
     *
38
     * @throws \Psr\Http\Client\ClientException
39
     *
40
     * @return mixed
41
     */
42
    public function send(string $method, array $data, array $files = [])
43
    {
44
        $request = $this->requestFactory->createRequest('POST', $this->generateUri($method));
45
46
        $boundary = \uniqid('', true);
47
48
        $stream = $this->streamFactory->createStream($this->createStreamBody($boundary, $data, $files));
49
50
        $response = $this->client->sendRequest($request
51
            ->withHeader('Content-Type', 'multipart/form-data; boundary="'.$boundary.'"')
52
            ->withBody($stream)
53
        );
54
55
        $content = $response->getBody()->getContents();
56
57
        return \json_decode($content);
58
    }
59
60
    public function setBotKey(string $botKey)
61
    {
62
        $this->botKey = $botKey;
63
    }
64
65
    public function setEndpoint(string $endPoint)
66
    {
67
        $this->endPoint = $endPoint;
68
    }
69
70
    /**
71
     * @param string $method
72
     *
73
     * @return string
74
     */
75
    protected function generateUri(string $method): string
76
    {
77
        return $this->endPoint . '/bot' . $this->botKey . '/' . $method;
78
    }
79
80
    /**
81
     * @param $data
82
     * @param $files
83
     * @param mixed $boundary
84
     *
85
     * @return string
86
     */
87
    protected function createStreamBody($boundary, $data, $files): string
88
    {
89
        $stream = '';
90
        foreach ($data as $name => $value) {
91
            $stream .= $this->createDataStream($boundary, $name, $value);
92
        }
93
94
        foreach ($files as $name => $file) {
95
            $stream .= $this->createFileStream($boundary, $name, $file);
96
        }
97
98
        $stream .= "--$boundary--\r\n";
99
100
        return $stream;
101
    }
102
103
    /**
104
     * @param $boundary
105
     * @param $name
106
     * @param \SplFileInfo $file
107
     *
108
     * @return string
109
     */
110
    protected function createFileStream($boundary, $name, \SplFileInfo $file): string
111
    {
112
        $headers = '';
113
        $headers .= \sprintf("Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n", $name, $file->getBasename());
114
        $headers .= \sprintf("Content-Length: %s\r\n", (string) $file->getSize());
115
        $headers .= \sprintf("Content-Type: %s\r\n", \mime_content_type($file->getRealPath()));
116
117
        $streams = '';
118
        $streams .= "--$boundary\r\n$headers\r\n";
119
        $streams .= \file_get_contents($file->getRealPath());
120
        $streams .= "\r\n";
121
122
        return $streams;
123
    }
124
125
    /**
126
     * @param $boundary
127
     * @param $name
128
     * @param $value
129
     *
130
     * @return string
131
     */
132
    protected function createDataStream($boundary, $name, $value): string
133
    {
134
        $headers = '';
135
        $headers .= \sprintf("Content-Disposition: form-data; name=\"%s\"\r\n", $name);
136
        $headers .= \sprintf("Content-Length: %s\r\n", (string) \strlen((string) $value));
137
138
        $streams = '';
139
        $streams .= "--$boundary\r\n$headers\r\n";
140
        $streams .= $value;
141
        $streams .= "\r\n";
142
143
        return $streams;
144
    }
145
}
146