1 | <?php |
||
19 | class Conversation |
||
20 | { |
||
21 | /** |
||
22 | * All information fetched from the database |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $conversation = null; |
||
27 | |||
28 | /** |
||
29 | * Notes stored inside the conversation |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $protected_notes = null; |
||
34 | |||
35 | /** |
||
36 | * Notes to be stored |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | public $notes = null; |
||
41 | |||
42 | /** |
||
43 | * Telegram user id |
||
44 | * |
||
45 | * @var int |
||
46 | */ |
||
47 | protected $user_id; |
||
48 | |||
49 | /** |
||
50 | * Telegram chat id |
||
51 | * |
||
52 | * @var int |
||
53 | */ |
||
54 | protected $chat_id; |
||
55 | |||
56 | /** |
||
57 | * Command to be executed if the conversation is active |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $command; |
||
62 | |||
63 | /** |
||
64 | * Conversation contructor to initialize a new conversation |
||
65 | * |
||
66 | * @param int $user_id |
||
67 | * @param int $chat_id |
||
68 | * @param string $command |
||
69 | */ |
||
70 | 9 | public function __construct($user_id, $chat_id, $command = null) |
|
71 | { |
||
72 | 9 | $this->user_id = $user_id; |
|
73 | 9 | $this->chat_id = $chat_id; |
|
74 | 9 | $this->command = $command; |
|
75 | |||
76 | //Try to load an existing conversation if possible |
||
77 | 9 | if (!$this->load() && $command !== null) { |
|
78 | //A new conversation start |
||
79 | 6 | $this->start(); |
|
80 | } |
||
81 | 8 | } |
|
82 | |||
83 | /** |
||
84 | * Clear all conversation variables. |
||
85 | * |
||
86 | * @return bool Always return true, to allow this method in an if statement. |
||
87 | */ |
||
88 | 2 | protected function clear() |
|
96 | |||
97 | /** |
||
98 | * Load the conversation from the database |
||
99 | * |
||
100 | * @return bool |
||
101 | */ |
||
102 | 9 | protected function load() |
|
103 | { |
||
104 | |||
105 | //Select an active conversation |
||
106 | 9 | $conversation = ConversationDB::selectConversation($this->user_id, $this->chat_id, 1); |
|
107 | 9 | if (isset($conversation[0])) { |
|
108 | //Pick only the first element |
||
109 | 5 | $this->conversation = $conversation[0]; |
|
110 | |||
111 | //Load the command from the conversation if it hasn't been passed |
||
112 | 5 | $this->command = $this->command ?: $this->conversation['command']; |
|
113 | |||
114 | 5 | if ($this->command !== $this->conversation['command']) { |
|
115 | $this->cancel(); |
||
116 | return false; |
||
117 | } |
||
118 | |||
119 | //Load the conversation notes |
||
120 | 5 | $this->protected_notes = json_decode($this->conversation['notes'], true); |
|
121 | 5 | $this->notes = $this->protected_notes; |
|
122 | } |
||
123 | |||
124 | 9 | return $this->exists(); |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * Check if the conversation already exists |
||
129 | * |
||
130 | * @return bool |
||
131 | */ |
||
132 | 9 | public function exists() |
|
136 | |||
137 | /** |
||
138 | * Start a new conversation if the current command doesn't have one yet |
||
139 | * |
||
140 | * @return bool |
||
141 | */ |
||
142 | 6 | protected function start() |
|
143 | { |
||
144 | 6 | if (!$this->exists() && $this->command) { |
|
145 | 6 | if (ConversationDB::insertConversation( |
|
146 | 6 | $this->user_id, |
|
147 | 6 | $this->chat_id, |
|
148 | 6 | $this->command |
|
149 | ) |
||
150 | ) { |
||
151 | 5 | return $this->load(); |
|
152 | } |
||
153 | } |
||
154 | |||
155 | return false; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Delete the current conversation |
||
160 | * |
||
161 | * Currently the Conversation is not deleted but just set to 'stopped' |
||
162 | * |
||
163 | * @return bool |
||
164 | */ |
||
165 | 1 | public function stop() |
|
169 | |||
170 | /** |
||
171 | * Cancel the current conversation |
||
172 | * |
||
173 | * @return bool |
||
174 | */ |
||
175 | 1 | public function cancel() |
|
179 | |||
180 | /** |
||
181 | * Update the status of the current conversation |
||
182 | * |
||
183 | * @param string $status |
||
184 | * |
||
185 | * @return bool |
||
186 | */ |
||
187 | 2 | protected function updateStatus($status) |
|
188 | { |
||
189 | 2 | if ($this->exists()) { |
|
190 | 2 | $fields = ['status' => $status]; |
|
191 | $where = [ |
||
192 | 2 | 'id' => $this->conversation['id'], |
|
193 | 2 | 'status' => 'active', |
|
194 | 2 | 'user_id' => $this->user_id, |
|
195 | 2 | 'chat_id' => $this->chat_id, |
|
196 | ]; |
||
197 | 2 | if (ConversationDB::updateConversation($fields, $where)) { |
|
198 | 2 | return true; |
|
199 | } |
||
200 | } |
||
201 | |||
202 | return false; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Store the array/variable in the database with json_encode() function |
||
207 | * |
||
208 | * @return bool |
||
209 | */ |
||
210 | 1 | public function update() |
|
211 | { |
||
212 | 1 | if ($this->exists()) { |
|
213 | 1 | $fields = ['notes' => json_encode($this->notes)]; |
|
214 | //I can update a conversation whatever the state is |
||
215 | 1 | $where = ['id' => $this->conversation['id']]; |
|
216 | 1 | if (ConversationDB::updateConversation($fields, $where)) { |
|
217 | 1 | return true; |
|
218 | } |
||
219 | } |
||
220 | |||
221 | return false; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Retrieve the command to execute from the conversation |
||
226 | * |
||
227 | * @return string|null |
||
228 | */ |
||
229 | 3 | public function getCommand() |
|
233 | } |
||
234 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..