1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Abstract entity event class is base class for creating the events for received in response entities. It |
5
|
|
|
* includes methods to be able to quickly get a chat id and reply on message, get parent entity which |
6
|
|
|
* triggered this entity event. |
7
|
|
|
* |
8
|
|
|
* @package Teebot (Telegram bot framework) |
9
|
|
|
* |
10
|
|
|
* @author Stanislav Drozdov <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace Teebot\Api\Command; |
16
|
|
|
|
17
|
|
|
use Teebot\Api\{ |
18
|
|
|
Response, |
19
|
|
|
Entity\EntityInterface, |
20
|
|
|
Entity\Message, |
21
|
|
|
Method\SendMessage |
22
|
|
|
}; |
23
|
|
|
|
24
|
|
|
abstract class AbstractEntityEvent implements EventInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var Processor $processor |
28
|
|
|
*/ |
29
|
|
|
protected $processor; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $params; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var EntityInterface $entity |
38
|
|
|
*/ |
39
|
|
|
protected $entity = null; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Abstract method of each entity event which will be called if certain entity presents in response |
43
|
|
|
* and it's class exists or mapped in the config. |
44
|
|
|
* |
45
|
|
|
* @return null|bool |
46
|
|
|
*/ |
47
|
|
|
abstract public function run(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Sets configuration parameters |
51
|
|
|
* |
52
|
|
|
* @param array $params |
53
|
|
|
* |
54
|
|
|
* @return EventInterface |
55
|
|
|
*/ |
56
|
|
|
public function setParams(array $params): EventInterface |
57
|
|
|
{ |
58
|
|
|
$this->params = $params; |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns chat id if the type of calling entity or it's parent is Message |
65
|
|
|
* |
66
|
|
|
* @return null|int |
67
|
|
|
*/ |
68
|
|
|
public function getChatId(): ?int |
69
|
|
|
{ |
70
|
|
|
if ($this->entity instanceof Message) { |
71
|
|
|
return $this->entity->getChatId(); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$parent = $this->entity->getParent(); |
75
|
|
|
|
76
|
|
|
if ($parent instanceof Message) { |
77
|
|
|
return $parent->getChatId(); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param Processor $processor |
85
|
|
|
* |
86
|
|
|
* @return EventInterface |
87
|
|
|
*/ |
88
|
|
|
public function setProcessor(Processor $processor): EventInterface |
89
|
|
|
{ |
90
|
|
|
$this->processor = $processor; |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Sets entity |
97
|
|
|
* |
98
|
|
|
* @param EntityInterface $entity Entity object |
99
|
|
|
* |
100
|
|
|
* @return EventInterface |
101
|
|
|
*/ |
102
|
|
|
public function setEntity(EntityInterface $entity): EventInterface |
103
|
|
|
{ |
104
|
|
|
$this->entity = $entity; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Sends text message to current chat |
111
|
|
|
* |
112
|
|
|
* @param string $text Message text |
113
|
|
|
* |
114
|
|
|
* @return Response |
115
|
|
|
*/ |
116
|
|
|
protected function sendMessage(string $text): Response |
117
|
|
|
{ |
118
|
|
|
return $this->reply( |
119
|
|
|
(new SendMessage()) |
120
|
|
|
->setText($text) |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Replies on the message |
126
|
|
|
* |
127
|
|
|
* @param SendMessage $sendMessage Object of the SendMessage method |
128
|
|
|
* |
129
|
|
|
* @return Response |
130
|
|
|
*/ |
131
|
|
|
protected function reply(SendMessage $sendMessage): Response |
132
|
|
|
{ |
133
|
|
|
$chatId = $this->getChatId(); |
134
|
|
|
|
135
|
|
|
if ((int)$chatId == 0) { |
136
|
|
|
return false; |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$sendMessage->setChatId($chatId); |
140
|
|
|
|
141
|
|
|
return $this->processor->call($sendMessage, true, $this->entity); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.