1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace FondBot\Drivers; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
use FondBot\Helpers\Arr; |
10
|
|
|
use FondBot\Queue\SerializableForQueue; |
11
|
|
|
use FondBot\Http\Request as HttpRequest; |
12
|
|
|
use FondBot\Drivers\Exceptions\InvalidRequest; |
13
|
|
|
|
14
|
|
|
abstract class Driver implements SerializableForQueue |
15
|
|
|
{ |
16
|
|
|
/** @var array */ |
17
|
|
|
protected $parameters; |
18
|
|
|
|
19
|
|
|
/** @var HttpRequest */ |
20
|
|
|
protected $request; |
21
|
|
|
|
22
|
|
|
/** @var Client */ |
23
|
|
|
protected $http; |
24
|
|
|
|
25
|
1 |
|
public function __construct(Client $http) |
26
|
|
|
{ |
27
|
1 |
|
$this->http = $http; |
28
|
1 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Fill driver with parameters and http request instance. |
32
|
|
|
* |
33
|
|
|
* @param array $parameters |
34
|
|
|
* @param HttpRequest $request |
35
|
|
|
*/ |
36
|
1 |
|
public function fill(array $parameters, HttpRequest $request): void |
37
|
|
|
{ |
38
|
1 |
|
$this->parameters = $parameters; |
39
|
1 |
|
$this->request = $request; |
40
|
1 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return Client |
44
|
|
|
*/ |
45
|
1 |
|
public function getHttp(): Client |
46
|
|
|
{ |
47
|
1 |
|
return $this->http; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get parameter value. |
52
|
|
|
* |
53
|
|
|
* @param string $name |
54
|
|
|
* @param null $default |
55
|
|
|
* |
56
|
|
|
* @return mixed |
57
|
|
|
*/ |
58
|
1 |
|
public function getParameter(string $name, $default = null) |
59
|
|
|
{ |
60
|
1 |
|
return Arr::get($this->parameters, $name, $default); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get template compiler instance. |
65
|
|
|
* |
66
|
|
|
* @return TemplateCompiler|null |
67
|
|
|
*/ |
68
|
|
|
abstract public function getTemplateCompiler(): ?TemplateCompiler; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get command handler instance. |
72
|
|
|
* |
73
|
|
|
* @return CommandHandler |
74
|
|
|
*/ |
75
|
|
|
abstract public function getCommandHandler(): CommandHandler; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Verify incoming request data. |
79
|
|
|
* |
80
|
|
|
* @throws InvalidRequest |
81
|
|
|
*/ |
82
|
|
|
abstract public function verifyRequest(): void; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get current chat. |
86
|
|
|
* |
87
|
|
|
* @return Chat |
88
|
|
|
*/ |
89
|
|
|
abstract public function getChat(): Chat; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get current user. |
93
|
|
|
* |
94
|
|
|
* @return User |
95
|
|
|
*/ |
96
|
|
|
abstract public function getUser(): User; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get message received from sender. |
100
|
|
|
* |
101
|
|
|
* @return ReceivedMessage |
102
|
|
|
*/ |
103
|
|
|
abstract public function getMessage(): ReceivedMessage; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Handle command. |
107
|
|
|
* |
108
|
|
|
* @param Command $command |
109
|
|
|
* |
110
|
|
|
* @throws RuntimeException |
111
|
|
|
*/ |
112
|
1 |
|
public function handle(Command $command): void |
113
|
|
|
{ |
114
|
1 |
|
$this->getCommandHandler()->handle($command); |
115
|
1 |
|
} |
116
|
|
|
} |
117
|
|
|
|