1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BotMan\BotMan\Drivers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use BotMan\BotMan\Interfaces\HttpInterface; |
7
|
|
|
use BotMan\BotMan\Interfaces\DriverInterface; |
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
9
|
|
|
use BotMan\BotMan\Messages\Incoming\IncomingMessage; |
10
|
|
|
|
11
|
|
|
abstract class HttpDriver implements DriverInterface |
12
|
|
|
{ |
13
|
|
|
/** @var Collection|ParameterBag */ |
14
|
|
|
protected $payload; |
15
|
|
|
|
16
|
|
|
/** @var Collection */ |
17
|
|
|
protected $event; |
18
|
|
|
|
19
|
|
|
/** @var HttpInterface */ |
20
|
|
|
protected $http; |
21
|
|
|
|
22
|
|
|
/** @var Collection */ |
23
|
|
|
protected $config; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $content; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Driver constructor. |
30
|
|
|
* @param Request $request |
31
|
|
|
* @param array $config |
32
|
|
|
* @param HttpInterface $http |
33
|
|
|
*/ |
34
|
|
|
final public function __construct(Request $request, array $config, HttpInterface $http) |
35
|
|
|
{ |
36
|
|
|
$this->http = $http; |
37
|
|
|
$this->config = Collection::make($config); |
38
|
|
|
$this->content = $request->getContent(); |
|
|
|
|
39
|
|
|
$this->buildPayload($request); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Return the driver name. |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function getName() |
48
|
|
|
{ |
49
|
|
|
return static::DRIVER_NAME; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Return the driver configuration. |
54
|
|
|
* |
55
|
|
|
* @return Collection |
56
|
|
|
*/ |
57
|
|
|
public function getConfig() |
58
|
|
|
{ |
59
|
|
|
return $this->config; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return the raw request content. |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function getContent() |
68
|
|
|
{ |
69
|
|
|
return $this->content; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param IncomingMessage $matchingMessage |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public function types(IncomingMessage $matchingMessage) |
77
|
|
|
{ |
78
|
|
|
// Do nothing |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Send a typing indicator and wait for the given amount of seconds. |
83
|
|
|
* @param IncomingMessage $matchingMessage |
84
|
|
|
* @param int $seconds |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
|
|
public function typesAndWaits(IncomingMessage $matchingMessage, int $seconds) |
88
|
|
|
{ |
89
|
|
|
$this->types($matchingMessage); |
90
|
|
|
sleep($seconds); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
public function hasMatchingEvent() |
97
|
|
|
{ |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param Request $request |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
abstract public function buildPayload(Request $request); |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Low-level method to perform driver specific API requests. |
109
|
|
|
* |
110
|
|
|
* @param string $endpoint |
111
|
|
|
* @param array $parameters |
112
|
|
|
* @param \BotMan\BotMan\Messages\Incoming\IncomingMessage $matchingMessage |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
abstract public function sendRequest($endpoint, array $parameters, IncomingMessage $matchingMessage); |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Tells if the stored conversation callbacks are serialized. |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
public function serializesCallbacks() |
123
|
|
|
{ |
124
|
|
|
return true; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.