1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace FondBot\Conversation\Traits; |
6
|
|
|
|
7
|
|
|
use FondBot\Drivers\Chat; |
8
|
|
|
use FondBot\Drivers\User; |
9
|
|
|
use FondBot\Contracts\Queue; |
10
|
|
|
use FondBot\Application\Kernel; |
11
|
|
|
use FondBot\Templates\Attachment; |
12
|
|
|
use FondBot\Conversation\Template; |
13
|
|
|
use FondBot\Drivers\Commands\SendMessage; |
14
|
|
|
use FondBot\Drivers\Commands\SendRequest; |
15
|
|
|
use FondBot\Drivers\Commands\SendAttachment; |
16
|
|
|
|
17
|
|
|
trait SendsMessages |
18
|
|
|
{ |
19
|
|
|
/** @var Kernel */ |
20
|
|
|
protected $kernel; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Send message to user. |
24
|
|
|
* |
25
|
|
|
* @param string $text |
26
|
|
|
* @param Template|null $template |
27
|
|
|
*/ |
28
|
2 |
View Code Duplication |
protected function sendMessage(string $text, Template $template = null): void |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
/** @var Queue $queue */ |
31
|
2 |
|
$queue = $this->kernel->resolve(Queue::class); |
32
|
|
|
|
33
|
2 |
|
$command = new SendMessage($this->getChat(), $this->getUser(), $text, $template); |
34
|
2 |
|
$queue->push($this->kernel->getDriver(), $command); |
35
|
2 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Send message to user with delay. |
39
|
|
|
* |
40
|
|
|
* @param int $delay |
41
|
|
|
* @param string $text |
42
|
|
|
* @param Template|null $template |
43
|
|
|
*/ |
44
|
1 |
View Code Duplication |
protected function sendDelayedMessage(int $delay, string $text, Template $template = null): void |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
/** @var Queue $queue */ |
47
|
1 |
|
$queue = $this->kernel->resolve(Queue::class); |
48
|
|
|
|
49
|
1 |
|
$command = new SendMessage($this->getChat(), $this->getUser(), $text, $template); |
50
|
1 |
|
$queue->later($this->kernel->getDriver(), $command, $delay); |
51
|
1 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Send attachment to user. |
55
|
|
|
* |
56
|
|
|
* @param Attachment $attachment |
57
|
|
|
* @param int $delay |
58
|
|
|
*/ |
59
|
2 |
|
protected function sendAttachment(Attachment $attachment, int $delay = 0): void |
60
|
|
|
{ |
61
|
|
|
/** @var Queue $queue */ |
62
|
2 |
|
$queue = $this->kernel->resolve(Queue::class); |
63
|
|
|
|
64
|
2 |
|
$command = new SendAttachment($this->getChat(), $this->getUser(), $attachment); |
65
|
|
|
|
66
|
2 |
|
if ($delay === 0) { |
67
|
1 |
|
$queue->push($this->kernel->getDriver(), $command); |
68
|
|
|
} else { |
69
|
1 |
|
$queue->later($this->kernel->getDriver(), $command, $delay); |
70
|
|
|
} |
71
|
2 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Send request to the messaging service. |
75
|
|
|
* |
76
|
|
|
* @param string $endpoint |
77
|
|
|
* @param array $parameters |
78
|
|
|
*/ |
79
|
1 |
View Code Duplication |
protected function sendRequest(string $endpoint, array $parameters = []): void |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
/** @var Queue $queue */ |
82
|
1 |
|
$queue = $this->kernel->resolve(Queue::class); |
83
|
|
|
|
84
|
1 |
|
$command = new SendRequest($this->getChat(), $this->getUser(), $endpoint, $parameters); |
85
|
1 |
|
$queue->push($this->kernel->getDriver(), $command); |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get chat. |
90
|
|
|
* |
91
|
|
|
* @return Chat |
92
|
|
|
*/ |
93
|
|
|
abstract protected function getChat(): Chat; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get user. |
97
|
|
|
* |
98
|
|
|
* @return User |
99
|
|
|
*/ |
100
|
|
|
abstract protected function getUser(): User; |
101
|
|
|
} |
102
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.