These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace BotMan\BotMan; |
||
4 | |||
5 | use React\Socket\Server; |
||
6 | use BotMan\BotMan\Http\Curl; |
||
7 | use React\EventLoop\LoopInterface; |
||
8 | use BotMan\BotMan\Cache\ArrayCache; |
||
9 | use BotMan\BotMan\Drivers\DriverManager; |
||
10 | use BotMan\BotMan\Interfaces\CacheInterface; |
||
11 | use Symfony\Component\HttpFoundation\Request; |
||
12 | use BotMan\BotMan\Interfaces\StorageInterface; |
||
13 | use BotMan\BotMan\Storages\Drivers\FileStorage; |
||
14 | |||
15 | class BotManFactory |
||
16 | { |
||
17 | private static $extensions = []; |
||
18 | |||
19 | /** |
||
20 | * @param $methodName |
||
21 | * @param $callable |
||
22 | */ |
||
23 | public static function extend($methodName, $callable) |
||
24 | { |
||
25 | self::$extensions[$methodName] = $callable; |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * @param string $name |
||
30 | * @param array $arguments |
||
31 | * @return mixed |
||
32 | */ |
||
33 | public static function __callStatic($name, $arguments) |
||
34 | { |
||
35 | try { |
||
36 | return call_user_func_array(self::$extensions[$name], $arguments); |
||
37 | } catch (\Exception $e) { |
||
38 | throw new \BadMethodCallException("Method [$name] does not exist."); |
||
39 | } |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Create a new BotMan instance. |
||
44 | * |
||
45 | * @param array $config |
||
46 | * @param CacheInterface $cache |
||
47 | * @param Request $request |
||
48 | * @param StorageInterface $storageDriver |
||
49 | * @return \BotMan\BotMan\BotMan |
||
50 | */ |
||
51 | public static function create( |
||
52 | array $config, |
||
53 | CacheInterface $cache = null, |
||
54 | Request $request = null, |
||
55 | StorageInterface $storageDriver = null |
||
56 | ) { |
||
57 | if (empty($cache)) { |
||
58 | $cache = new ArrayCache(); |
||
59 | } |
||
60 | if (empty($request)) { |
||
61 | $request = Request::createFromGlobals(); |
||
62 | } |
||
63 | if (empty($storageDriver)) { |
||
64 | $storageDriver = new FileStorage(__DIR__); |
||
65 | } |
||
66 | |||
67 | $driverManager = new DriverManager($config, new Curl()); |
||
68 | $driver = $driverManager->getMatchingDriver($request); |
||
69 | |||
70 | return new BotMan($cache, $driver, $config, $storageDriver); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Create a new BotMan instance that listens on a socket. |
||
75 | * |
||
76 | * @param array $config |
||
77 | * @param LoopInterface $loop |
||
78 | * @param CacheInterface $cache |
||
79 | * @param StorageInterface $storageDriver |
||
80 | * @return \BotMan\BotMan\BotMan |
||
81 | */ |
||
82 | public static function createForSocket( |
||
83 | array $config, |
||
84 | LoopInterface $loop, |
||
85 | CacheInterface $cache = null, |
||
86 | StorageInterface $storageDriver = null |
||
87 | ) { |
||
88 | $port = isset($config['port']) ? $config['port'] : 8080; |
||
89 | |||
90 | $socket = new Server($loop); |
||
0 ignored issues
–
show
|
|||
91 | |||
92 | if (empty($cache)) { |
||
93 | $cache = new ArrayCache(); |
||
94 | } |
||
95 | |||
96 | if (empty($storageDriver)) { |
||
97 | $storageDriver = new FileStorage(__DIR__); |
||
98 | } |
||
99 | |||
100 | $driverManager = new DriverManager($config, new Curl()); |
||
101 | |||
102 | $botman = new BotMan($cache, DriverManager::loadFromName('Null', $config), $config, $storageDriver); |
||
103 | $botman->runsOnSocket(true); |
||
104 | |||
105 | $socket->on('connection', function ($conn) use ($botman, $driverManager) { |
||
106 | $conn->on('data', function ($data) use ($botman, $driverManager) { |
||
107 | $requestData = json_decode($data, true); |
||
108 | $request = new Request($requestData['query'], $requestData['request'], $requestData['attributes'], [], [], [], $requestData['content']); |
||
109 | $driver = $driverManager->getMatchingDriver($request); |
||
110 | $botman->setDriver($driver); |
||
111 | $botman->listen(); |
||
112 | }); |
||
113 | }); |
||
114 | $socket->listen($port); |
||
0 ignored issues
–
show
|
|||
115 | |||
116 | return $botman; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Pass an incoming HTTP request to the socket. |
||
121 | * |
||
122 | * @param int $port The port to use. Default is 8080 |
||
123 | * @param Request|null $request |
||
124 | * @return void |
||
125 | */ |
||
126 | public static function passRequestToSocket($port = 8080, Request $request = null) |
||
127 | { |
||
128 | if (empty($request)) { |
||
129 | $request = Request::createFromGlobals(); |
||
130 | } |
||
131 | |||
132 | $client = stream_socket_client('tcp://127.0.0.1:'.$port); |
||
133 | fwrite($client, json_encode([ |
||
134 | 'attributes' => $request->attributes->all(), |
||
135 | 'query' => $request->query->all(), |
||
136 | 'request' => $request->request->all(), |
||
137 | 'content' => $request->getContent(), |
||
138 | ])); |
||
139 | fclose($client); |
||
140 | } |
||
141 | } |
||
142 |
This check looks for function calls that miss required arguments.