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