1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Zanzara\UpdateMode; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use React\EventLoop\LoopInterface; |
9
|
|
|
use Zanzara\Config; |
10
|
|
|
use Zanzara\Context; |
11
|
|
|
use Zanzara\Listener\Listener; |
12
|
|
|
use Zanzara\Telegram\Telegram; |
13
|
|
|
use Zanzara\Telegram\Type\Update; |
14
|
|
|
use Zanzara\Zanzara; |
15
|
|
|
use Zanzara\ZanzaraLogger; |
16
|
|
|
use Zanzara\ZanzaraMapper; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
abstract class UpdateMode implements UpdateModeInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ContainerInterface |
26
|
|
|
*/ |
27
|
|
|
protected $container; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Zanzara |
31
|
|
|
*/ |
32
|
|
|
protected $zanzara; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Telegram |
36
|
|
|
*/ |
37
|
|
|
protected $telegram; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Config |
41
|
|
|
*/ |
42
|
|
|
protected $config; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var ZanzaraLogger |
46
|
|
|
*/ |
47
|
|
|
protected $logger; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var LoopInterface |
51
|
|
|
*/ |
52
|
|
|
protected $loop; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var ZanzaraMapper |
56
|
|
|
*/ |
57
|
|
|
protected $zanzaraMapper; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param ContainerInterface $container |
61
|
|
|
* @param Zanzara $zanzara |
62
|
|
|
* @param Telegram $telegram |
63
|
|
|
* @param Config $config |
64
|
|
|
* @param ZanzaraLogger $logger |
65
|
|
|
* @param LoopInterface $loop |
66
|
|
|
* @param ZanzaraMapper $zanzaraMapper |
67
|
|
|
*/ |
68
|
|
|
public function __construct(ContainerInterface $container, Zanzara $zanzara, Telegram $telegram, Config $config, |
69
|
|
|
ZanzaraLogger $logger, LoopInterface $loop, ZanzaraMapper $zanzaraMapper) |
70
|
|
|
{ |
71
|
|
|
$this->container = $container; |
72
|
|
|
$this->zanzara = $zanzara; |
73
|
|
|
$this->telegram = $telegram; |
74
|
|
|
$this->config = $config; |
75
|
|
|
$this->logger = $logger; |
76
|
|
|
$this->loop = $loop; |
77
|
|
|
$this->zanzaraMapper = $zanzaraMapper; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param Update $update |
82
|
|
|
*/ |
83
|
|
|
protected function processUpdate(Update $update) |
84
|
|
|
{ |
85
|
|
|
$update->detectUpdateType(); |
86
|
|
|
$this->zanzara->resolveListeners($update) |
87
|
|
|
->then(function ($listeners) use ($update) { |
88
|
|
|
/** @var Listener[] $listeners */ |
89
|
|
|
foreach ($listeners as $listener) { |
90
|
|
|
$context = new Context($update, $this->container); |
91
|
|
|
$middlewareTip = $listener->getTip(); |
92
|
|
|
$middlewareTip($context); |
93
|
|
|
} |
94
|
|
|
})->otherwise(function ($e) use ($update) { |
|
|
|
|
95
|
|
|
$this->logger->error("Unable to resolve listeners for update $update, reason: $e"); |
96
|
|
|
}); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|