UpdateBot::getUpdates()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zored\Telegram\Bot;
6
7
use Zored\Telegram\Entity\Bot\Update;
8
use Zored\Telegram\TelegramApiInterface;
9
use Zored\Telegram\Util\Repeater\Repeater;
10
use Zored\Telegram\Util\Repeater\RepeaterInterface;
11
12
class UpdateBot implements BotInterface
13
{
14
    /**
15
     * @var TelegramApiInterface
16
     */
17
    private $api;
18
19
    /**
20
     * @var RepeaterInterface
21
     */
22
    private $repeater;
23
24
    public function __construct(TelegramApiInterface $api, RepeaterInterface $repeater = null)
25
    {
26
        $this->api = $api;
27
        $this->repeater = $repeater ?: new Repeater(
28
            1000, // 1 second
29
            1000 * 60 * 5 // 5 minutes
30
        );
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function listen(array $updateHandlers): void
37
    {
38
        $this->repeater->repeat(function () use ($updateHandlers) {
39
            foreach ($this->getUpdates() as $update) {
40
                foreach ($updateHandlers as $handler) {
41
                    $handler->handle($update);
42
                }
43
            }
44
        });
45
    }
46
47
    /**
48
     * @return Update[]
49
     *
50
     * @throws \Zored\Telegram\Exception\TelegramApiException
51
     */
52
    private function getUpdates(): array
53
    {
54
        return $this->api->getUpdates();
55
    }
56
}
57