TelegramApi::getDialogs()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zored\Telegram;
6
7
use danog\MadelineProto\API;
8
use danog\MadelineProto\MTProto;
9
use danog\MadelineProto\RPCErrorException;
10
use Zored\Telegram\Entity\Bot\Update;
11
use Zored\Telegram\Entity\Bot\Update\ShortSentMessage;
12
use Zored\Telegram\Entity\Contacts;
13
use Zored\Telegram\Entity\Control\Message\MessageInterface;
14
use Zored\Telegram\Entity\Control\Peer\PeerInterface;
15
use Zored\Telegram\Entity\Dialogs;
16
use Zored\Telegram\Entity\User;
17
use Zored\Telegram\Exception\TelegramApiException;
18
use Zored\Telegram\Exception\TelegramApiLogicException;
19
use Zored\Telegram\Serializer\SerializerInterface;
20
21
class TelegramApi implements TelegramApiInterface
22
{
23
    /**
24
     * @var API
25
     */
26
    private $api;
27
28
    /**
29
     * @var MTProto
30
     */
31
    private $proto;
32
33
    /**
34
     * @var SerializerInterface
35
     */
36
    private $serializer;
37
38
    /**
39
     * @var int
40
     */
41
    private $nextUpdateId = 0;
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function __construct(API $api, SerializerInterface $serializer)
47
    {
48
        $this->api = $api;
49
        $this->proto = $api->API;
50
        $this->serializer = $serializer;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getContacts(): Contacts
57
    {
58
        /** @var array $response */
59
        $response = $this->api->contacts->getContacts(['hash' => 0]);
60
61
        $contacts = $this->hydrate(Contacts::class, $response);
62
        if (!$contacts instanceof Contacts) {
63
            throw TelegramApiLogicException::becauseOfWrongType($contacts, Contacts::class);
64
        }
65
66
        return $contacts;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function sendMessage(
73
        PeerInterface $peer,
74
        MessageInterface $message,
75
        array $etc = []
76
    ): ShortSentMessage {
77
        /** @var array $response */
78
        $response = $this->api->messages->sendMessage(array_merge([
79
            'peer' => $peer->getType() . '#' . $peer->getId(),
80
            'message' => $message->getContent(),
81
            'parse_mode' => $message->getFormat(),
82
            'disable_web_page_preview' => $message->isLinkPreview(),
83
        ], $etc));
84
85
        $shortSentMessage = $this->hydrate(ShortSentMessage::class, $response);
86
        if (!$shortSentMessage instanceof ShortSentMessage) {
87
            throw TelegramApiLogicException::becauseOfWrongType($shortSentMessage, ShortSentMessage::class);
88
        }
89
90
        return $shortSentMessage;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function getChats(): array
97
    {
98
        // $this->api->messages->getChats(['id' => 0]);
99
100
        // TODO: implement
101
        return [];
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function getDialogs(): Dialogs
108
    {
109
        /** @var array $response */
110
        $response = $this->api->messages->getDialogs([
111
            'offset_date' => 0,
112
            'offset_id' => 0,
113
            'limit' => 100,
114
        ]);
115
116
        $dialogs = $this->hydrate(Dialogs::class, $response);
117
        if (!$dialogs instanceof Dialogs) {
118
            throw TelegramApiLogicException::becauseOfWrongType($dialogs, Dialogs::class);
119
        }
120
121
        return $dialogs;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function getCurrentUser(): User
128
    {
129
        $response = $this->proto->get_self() ?: [];
130
131
        $user = $this->hydrate(User::class, $response);
132
        if (!$user instanceof User) {
133
            throw TelegramApiLogicException::becauseOfWrongType($user, User::class);
134
        }
135
136
        return $user;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function getUpdates(int $offset = null, int $limit = 50, int $interval = 10): array
143
    {
144
        $offset = $offset ?? $this->nextUpdateId;
145
        try {
146
            $updates = $this->proto->get_updates([
147
                'offset' => $offset,
148
                'limit' => $limit,
149
                'timeout' => $interval,
150
            ]);
151
        } catch (RPCErrorException $apiException) {
152
            throw TelegramApiException::becauseOfApiException($apiException);
153
        }
154
155
        return array_map([$this, 'createUpdate'], $updates);
156
    }
157
158
    /**
159
     * @throws TelegramApiLogicException
160
     */
161
    private function createUpdate(array $data): Update
162
    {
163
        $update = $this->hydrate(Update::class, $data);
164
        if (!$update instanceof Update) {
165
            throw TelegramApiLogicException::becauseOfWrongType($update, Update::class);
166
        }
167
168
        $this->nextUpdateId = $update->getUpdateId() + 1;
169
170
        return $update;
171
    }
172
173
    /**
174
     * @param string $class
175
     * @param array $response
176
     *
177
     * @return object
178
     */
179
    private function hydrate(string $class, array $response)
180
    {
181
        return $this->serializer->deserialize($class, $response);
182
    }
183
}
184