Completed
Push — master ( 5a8248...de4c72 )
by Nikolay
03:57
created

BotApi::exportChatInviteLink()   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 2
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 TgBotApi\BotApiBase;
6
7
use TgBotApi\BotApiBase\Exception\ResponseException;
8
use TgBotApi\BotApiBase\Method\ExportChatInviteLinkMethod;
9
use TgBotApi\BotApiBase\Method\SendChatActionMethod;
10
use TgBotApi\BotApiBase\Method\SendMediaGroupMethod;
11
use TgBotApi\BotApiBase\Traits\AliasMethodTrait;
12
use TgBotApi\BotApiBase\Traits\GetMethodTrait;
13
use TgBotApi\BotApiBase\Type\FileType;
14
use TgBotApi\BotApiBase\Type\MessageType;
15
16
/**
17
 * Class BotApi.
18
 */
19
class BotApi implements BotApiInterface
20
{
21
    use AliasMethodTrait;
22
    use GetMethodTrait;
23
    /**
24
     * @var string
25
     */
26
    private $botKey;
27
28
    /**
29
     * @var ApiClientInterface
30
     */
31
    private $apiClient;
32
33
    /**
34
     * @var string
35
     */
36
    private $endPoint;
37
38
    /**
39
     * @var NormalizerInterface
40
     */
41
    private $normalizer;
42
43
    /**
44
     * BotApi constructor.
45
     *
46
     * @param string              $botKey
47
     * @param ApiClientInterface  $apiClient
48
     * @param NormalizerInterface $normalizer
49
     * @param string              $endPoint
50
     */
51 67
    public function __construct(
52
        string $botKey,
53
        ApiClientInterface $apiClient,
54
        NormalizerInterface $normalizer,
55
        string $endPoint = 'https://api.telegram.org'
56
    ) {
57 67
        $this->botKey = $botKey;
58 67
        $this->apiClient = $apiClient;
59 67
        $this->normalizer = $normalizer;
60 67
        $this->endPoint = $endPoint;
61
62 67
        $this->apiClient->setBotKey($botKey);
63 67
        $this->apiClient->setEndpoint($endPoint);
64 67
    }
65
66
    /**
67
     * @param $method
68
     * @param string|null $type
69
     *
70
     * @throws ResponseException
71
     *
72
     * @return mixed
73
     */
74 66
    public function call($method, string $type = null)
75
    {
76 66
        $json = $this->apiClient->send($this->getMethodName($method), $this->normalizer->normalize($method));
77
78 66
        if (true !== $json->ok) {
79
            throw new ResponseException($json->description);
80
        }
81
82 66
        return $type ? $this->normalizer->denormalize($json, $type) : $json->result;
83
    }
84
85
    /**
86
     * @param ExportChatInviteLinkMethod $method
87
     *
88
     * @throws ResponseException
89
     *
90
     * @return string
91
     */
92
    public function exportChatInviteLink(ExportChatInviteLinkMethod $method): string
93
    {
94
        return $this->call($method);
95
    }
96
97
    /**
98
     * @param SendChatActionMethod $method
99
     *
100
     * @throws ResponseException
101
     *
102
     * @return bool
103
     */
104 1
    public function sendChatAction(SendChatActionMethod $method): bool
105
    {
106 1
        return $this->call($method);
107
    }
108
109
    /**
110
     * @param SendMediaGroupMethod $method
111
     *
112
     * @throws ResponseException
113
     *
114
     * @return MessageType[]
115
     */
116 2
    public function sendMediaGroup(SendMediaGroupMethod $method): array
117
    {
118 2
        return $this->call($method, MessageType::class . '[]');
119
    }
120
121
    /**
122
     * @param FileType $file
123
     *
124
     * @return string
125
     */
126 1
    public function getAbsoluteFilePath(FileType $file): string
127
    {
128 1
        return \sprintf(
129 1
            '%s/file/bot%s/%s',
130 1
            $this->endPoint,
131 1
            $this->botKey,
132 1
            $file->filePath
133
        );
134
    }
135
136
    /**
137
     * @param $method
138
     *
139
     * @return string
140
     */
141 66
    private function getMethodName($method): string
142
    {
143 66
        return \lcfirst(\substr(
144 66
            \get_class($method),
145 66
            \strrpos(\get_class($method), '\\') + 1,
146 66
            -1 * \strlen('Method')
147
        ));
148
    }
149
}
150