|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace FondBot\Drivers; |
|
6
|
|
|
|
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
use GuzzleHttp\Client; |
|
9
|
|
|
use Illuminate\Http\Request; |
|
10
|
|
|
use FondBot\Channels\Channel; |
|
11
|
|
|
use Illuminate\Support\Collection; |
|
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
13
|
|
|
use FondBot\Drivers\Exceptions\InvalidRequest; |
|
14
|
|
|
use FondBot\Contracts\Driver as DriverContract; |
|
15
|
|
|
|
|
16
|
|
|
abstract class Driver implements DriverContract |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var Collection */ |
|
19
|
|
|
protected $parameters; |
|
20
|
|
|
|
|
21
|
|
|
/** @var Request */ |
|
22
|
|
|
protected $request; |
|
23
|
|
|
|
|
24
|
|
|
protected $guzzle; |
|
25
|
|
|
|
|
26
|
3 |
|
public function __construct(Client $guzzle) |
|
27
|
|
|
{ |
|
28
|
3 |
|
$this->guzzle = $guzzle; |
|
29
|
3 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Get driver short name. |
|
33
|
|
|
* |
|
34
|
|
|
* This name is used as an alias for configuration. |
|
35
|
|
|
* |
|
36
|
|
|
* @return string |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function getShortName(): string |
|
39
|
|
|
{ |
|
40
|
1 |
|
$shortName = explode('\\', get_class($this)); |
|
41
|
|
|
|
|
42
|
1 |
|
return collect($shortName)->last(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Initialize gateway with parameters. |
|
47
|
|
|
* |
|
48
|
|
|
* @param Channel $channel |
|
49
|
|
|
* @param Request $request |
|
50
|
|
|
* |
|
51
|
|
|
* @return Driver|DriverContract|static |
|
52
|
|
|
*/ |
|
53
|
2 |
|
public function initialize(Channel $channel, Request $request): DriverContract |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
2 |
|
$this->request = $request; |
|
56
|
|
|
|
|
57
|
2 |
|
$parameters = []; |
|
58
|
|
|
|
|
59
|
2 |
|
foreach ($this->getDefaultParameters() as $key => $value) { |
|
60
|
2 |
|
$value = array_get($channel->getParameters(), $key, $value); |
|
61
|
|
|
|
|
62
|
2 |
|
array_set($parameters, $key, $value); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
2 |
|
$this->parameters = collect($parameters); |
|
66
|
|
|
|
|
67
|
2 |
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get parameters. |
|
72
|
|
|
* |
|
73
|
|
|
* @return Collection |
|
74
|
|
|
*/ |
|
75
|
2 |
|
public function getParameters(): Collection |
|
76
|
|
|
{ |
|
77
|
2 |
|
return $this->parameters; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get request. |
|
82
|
|
|
* |
|
83
|
|
|
* @return Request |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getRequest(): Request |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->request; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Send a GET request. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $uri |
|
94
|
|
|
* @param array $options |
|
95
|
|
|
* |
|
96
|
|
|
* @return ResponseInterface |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function get(string $uri, array $options = []): ResponseInterface |
|
99
|
|
|
{ |
|
100
|
1 |
|
return $this->guzzle->get($uri, $options); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Send a POST request. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $uri |
|
107
|
|
|
* @param array $options |
|
108
|
|
|
* |
|
109
|
|
|
* @return ResponseInterface |
|
110
|
|
|
*/ |
|
111
|
1 |
|
public function post(string $uri, array $options = []): ResponseInterface |
|
112
|
|
|
{ |
|
113
|
1 |
|
return $this->guzzle->post($uri, $options); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Get template compiler instance. |
|
118
|
|
|
* |
|
119
|
|
|
* @return TemplateCompiler|null |
|
120
|
|
|
*/ |
|
121
|
|
|
abstract public function getTemplateCompiler(): ?TemplateCompiler; |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Get command handler instance. |
|
125
|
|
|
* |
|
126
|
|
|
* @return CommandHandler |
|
127
|
|
|
*/ |
|
128
|
|
|
abstract public function getCommandHandler(): CommandHandler; |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Verify request consistency. |
|
132
|
|
|
* |
|
133
|
|
|
* @throws InvalidRequest |
|
134
|
|
|
*/ |
|
135
|
|
|
abstract public function verifyRequest(): void; |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Get current chat. |
|
139
|
|
|
* |
|
140
|
|
|
* @return Chat |
|
141
|
|
|
*/ |
|
142
|
|
|
abstract public function getChat(): Chat; |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get current user. |
|
146
|
|
|
* |
|
147
|
|
|
* @return User |
|
148
|
|
|
*/ |
|
149
|
|
|
abstract public function getUser(): User; |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get message received from sender. |
|
153
|
|
|
* |
|
154
|
|
|
* @return ReceivedMessage |
|
155
|
|
|
*/ |
|
156
|
|
|
abstract public function getMessage(): ReceivedMessage; |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Handle command. |
|
160
|
|
|
* |
|
161
|
|
|
* @param Command $command |
|
162
|
|
|
* |
|
163
|
|
|
* @throws RuntimeException |
|
164
|
|
|
*/ |
|
165
|
1 |
|
public function handle(Command $command): void |
|
166
|
|
|
{ |
|
167
|
1 |
|
$this->getCommandHandler()->handle($command); |
|
168
|
1 |
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|