@@ -42,7 +42,7 @@ |
||
42 | 42 | $data['raw_data'] = $data; |
43 | 43 | $this->response = $data; |
44 | 44 | |
45 | - $is_ok = (bool)($data['ok'] ?? false); |
|
45 | + $is_ok = (bool) ($data['ok'] ?? false); |
|
46 | 46 | $result = $data['result'] ?? null; |
47 | 47 | |
48 | 48 | if ($is_ok) { |
@@ -37,7 +37,7 @@ |
||
37 | 37 | |
38 | 38 | if ($these_photos = $this->getProperty('photos')) { |
39 | 39 | foreach ($these_photos as $photos) { |
40 | - $all_photos[] = array_map(function ($photo) { |
|
40 | + $all_photos[] = array_map(function($photo) { |
|
41 | 41 | return new PhotoSize($photo); |
42 | 42 | }, $photos); |
43 | 43 | } |
@@ -50,7 +50,9 @@ |
||
50 | 50 | */ |
51 | 51 | protected function getEnvToken(string $file): string|null |
52 | 52 | { |
53 | - if (!file_exists($file)) return null; |
|
53 | + if (!file_exists($file)) { |
|
54 | + return null; |
|
55 | + } |
|
54 | 56 | return $_ENV['TELEGRAM_BOT_TOKEN'] ?? null; |
55 | 57 | } |
56 | 58 |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | * |
28 | 28 | * @return string|null |
29 | 29 | */ |
30 | - protected static function guessEnvPath(): string|null |
|
30 | + protected static function guessEnvPath(): string|null |
|
31 | 31 | { |
32 | 32 | $defaultEnvPaths = [ |
33 | 33 | getcwd() . '/.env', |
@@ -45,10 +45,10 @@ discard block |
||
45 | 45 | } |
46 | 46 | |
47 | 47 | protected static function tryAutoloadEnv(): void { |
48 | - $envPath = static::guessEnvPath(); |
|
49 | - if ($envPath !== null) { |
|
48 | + $envPath = static::guessEnvPath(); |
|
49 | + if ($envPath !== null) { |
|
50 | 50 | (new Dotenv())->load($envPath); |
51 | - } |
|
51 | + } |
|
52 | 52 | } |
53 | 53 | |
54 | 54 | /** |
@@ -107,7 +107,9 @@ discard block |
||
107 | 107 | public function fetchWith(UpdateHandler $update_handler, Update|null $update = null): void |
108 | 108 | { |
109 | 109 | if (is_subclass_of($update_handler, UpdateHandler::class)) { |
110 | - if ($update === null) $update = self::getUpdate(); |
|
110 | + if ($update === null) { |
|
111 | + $update = self::getUpdate(); |
|
112 | + } |
|
111 | 113 | $update_handler->resolve($update); |
112 | 114 | } |
113 | 115 | } |
@@ -120,7 +122,9 @@ discard block |
||
120 | 122 | public static function getUpdate(): Update|null |
121 | 123 | { |
122 | 124 | $input = self::getInput(); |
123 | - if (empty($input)) return null; |
|
125 | + if (empty($input)) { |
|
126 | + return null; |
|
127 | + } |
|
124 | 128 | return Telegram::processUpdate($input, self::getApiToken()); |
125 | 129 | } |
126 | 130 |
@@ -115,7 +115,7 @@ |
||
115 | 115 | { |
116 | 116 | // Get the correct logger instance. |
117 | 117 | $logger = null; |
118 | - if (in_array($name, ['emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug',], true)) { |
|
118 | + if (in_array($name, ['emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug', ], true)) { |
|
119 | 119 | $logger = self::$logger; |
120 | 120 | } elseif ($name === 'update') { |
121 | 121 | $logger = self::$update_logger; |
@@ -10,57 +10,57 @@ |
||
10 | 10 | |
11 | 11 | class WebhookTest extends \PHPUnit\Framework\TestCase { |
12 | 12 | |
13 | - public function testAnonymousPlugins() { |
|
14 | - $plugin = new class($this) extends Plugin { |
|
13 | + public function testAnonymousPlugins() { |
|
14 | + $plugin = new class($this) extends Plugin { |
|
15 | 15 | |
16 | - private $testCase; |
|
16 | + private $testCase; |
|
17 | 17 | |
18 | - public function __construct(TestCase $testCase) { |
|
18 | + public function __construct(TestCase $testCase) { |
|
19 | 19 | $this->testCase = $testCase; |
20 | - } |
|
20 | + } |
|
21 | 21 | |
22 | - public function onMessage(int $update_id, Message $message): \Generator { |
|
22 | + public function onMessage(int $update_id, Message $message): \Generator { |
|
23 | 23 | $this->testCase->assertEquals(1, $update_id); |
24 | 24 | $this->testCase->assertEquals('Hello World!', $message->getText()); |
25 | 25 | yield; |
26 | - } |
|
26 | + } |
|
27 | 27 | |
28 | - }; |
|
28 | + }; |
|
29 | 29 | |
30 | - $message = DummyUpdate::message(); |
|
31 | - $message->set('text', 'Hello World!'); |
|
30 | + $message = DummyUpdate::message(); |
|
31 | + $message->set('text', 'Hello World!'); |
|
32 | 32 | |
33 | - (new UpdateHandler())->addPlugins($plugin)->resolve(new Update([ |
|
34 | - 'update_id' => 1, |
|
35 | - 'message' => $message->getRawData(), |
|
36 | - ])); |
|
37 | - } |
|
33 | + (new UpdateHandler())->addPlugins($plugin)->resolve(new Update([ |
|
34 | + 'update_id' => 1, |
|
35 | + 'message' => $message->getRawData(), |
|
36 | + ])); |
|
37 | + } |
|
38 | 38 | |
39 | - public function testFilterIncomingUpdates() { |
|
39 | + public function testFilterIncomingUpdates() { |
|
40 | 40 | |
41 | - $plugin = new class($this) extends Plugin { |
|
41 | + $plugin = new class($this) extends Plugin { |
|
42 | 42 | |
43 | - public function __construct(TestCase $testCase) { |
|
43 | + public function __construct(TestCase $testCase) { |
|
44 | 44 | $this->testCase = $testCase; |
45 | - } |
|
45 | + } |
|
46 | 46 | |
47 | - public function __process(Update $update): void { |
|
47 | + public function __process(Update $update): void { |
|
48 | 48 | $this->testCase->fail('This should not be called'); |
49 | - } |
|
50 | - }; |
|
49 | + } |
|
50 | + }; |
|
51 | 51 | |
52 | - $handler = (new UpdateHandler())->addPlugins($plugin); |
|
52 | + $handler = (new UpdateHandler())->addPlugins($plugin); |
|
53 | 53 | |
54 | - $handler->filterIncomingUpdates([ |
|
55 | - Update::TYPE_MESSAGE |
|
56 | - ]); |
|
54 | + $handler->filterIncomingUpdates([ |
|
55 | + Update::TYPE_MESSAGE |
|
56 | + ]); |
|
57 | 57 | |
58 | - $handler->resolve(new Update([ |
|
59 | - 'update_id' => 1, |
|
60 | - 'message' => DummyUpdate::message()->getRawData(), |
|
61 | - ])); |
|
58 | + $handler->resolve(new Update([ |
|
59 | + 'update_id' => 1, |
|
60 | + 'message' => DummyUpdate::message()->getRawData(), |
|
61 | + ])); |
|
62 | 62 | |
63 | - $this->assertTrue(true); |
|
64 | - } |
|
63 | + $this->assertTrue(true); |
|
64 | + } |
|
65 | 65 | |
66 | 66 | } |
67 | 67 | \ No newline at end of file |
@@ -9,114 +9,114 @@ |
||
9 | 9 | class DummyUpdate { |
10 | 10 | |
11 | 11 | |
12 | - private static function faker(): Generator { |
|
13 | - return Factory::create(); |
|
14 | - } |
|
12 | + private static function faker(): Generator { |
|
13 | + return Factory::create(); |
|
14 | + } |
|
15 | 15 | |
16 | - public static function message(): Message { |
|
17 | - return new Message([ |
|
18 | - 'message_id' => self::faker()->randomNumber(), |
|
19 | - 'from' => [ |
|
16 | + public static function message(): Message { |
|
17 | + return new Message([ |
|
18 | + 'message_id' => self::faker()->randomNumber(), |
|
19 | + 'from' => [ |
|
20 | 20 | 'id' => self::faker()->randomNumber(), |
21 | 21 | 'is_bot' => false, |
22 | 22 | 'first_name' => self::faker()->firstName(), |
23 | 23 | 'last_name' => self::faker()->lastName(), |
24 | 24 | 'username' => self::faker()->userName(), |
25 | 25 | 'language_code' => self::faker()->languageCode(), |
26 | - ], |
|
27 | - 'chat' => [ |
|
26 | + ], |
|
27 | + 'chat' => [ |
|
28 | 28 | 'id' => self::faker()->randomNumber(), |
29 | 29 | 'first_name' => self::faker()->firstName(), |
30 | 30 | 'last_name' => self::faker()->lastName(), |
31 | 31 | 'username' => self::faker()->userName(), |
32 | 32 | 'type' => 'private', |
33 | - ], |
|
34 | - 'date' => self::faker()->unixTime(), |
|
35 | - 'text' => self::faker()->sentence(), |
|
36 | - ]); |
|
37 | - } |
|
33 | + ], |
|
34 | + 'date' => self::faker()->unixTime(), |
|
35 | + 'text' => self::faker()->sentence(), |
|
36 | + ]); |
|
37 | + } |
|
38 | 38 | |
39 | - public static function messageWithEntities(): Message { |
|
40 | - return new Message([ |
|
41 | - 'message_id' => self::faker()->randomNumber(), |
|
42 | - 'from' => [ |
|
39 | + public static function messageWithEntities(): Message { |
|
40 | + return new Message([ |
|
41 | + 'message_id' => self::faker()->randomNumber(), |
|
42 | + 'from' => [ |
|
43 | 43 | 'id' => self::faker()->randomNumber(), |
44 | 44 | 'is_bot' => false, |
45 | 45 | 'first_name' => self::faker()->firstName(), |
46 | 46 | 'last_name' => self::faker()->lastName(), |
47 | 47 | 'username' => self::faker()->userName(), |
48 | 48 | 'language_code' => self::faker()->languageCode(), |
49 | - ], |
|
50 | - 'chat' => [ |
|
49 | + ], |
|
50 | + 'chat' => [ |
|
51 | 51 | 'id' => self::faker()->randomNumber(), |
52 | 52 | 'first_name' => self::faker()->firstName(), |
53 | 53 | 'last_name' => self::faker()->lastName(), |
54 | 54 | 'username' => self::faker()->userName(), |
55 | 55 | 'type' => 'private', |
56 | - ], |
|
57 | - 'date' => self::faker()->unixTime(), |
|
58 | - 'text' => self::faker()->sentence(), |
|
59 | - 'entities' => [ |
|
56 | + ], |
|
57 | + 'date' => self::faker()->unixTime(), |
|
58 | + 'text' => self::faker()->sentence(), |
|
59 | + 'entities' => [ |
|
60 | 60 | [ |
61 | - 'offset' => 0, |
|
62 | - 'length' => 1, |
|
63 | - 'type' => 'bold', |
|
61 | + 'offset' => 0, |
|
62 | + 'length' => 1, |
|
63 | + 'type' => 'bold', |
|
64 | 64 | ], |
65 | 65 | [ |
66 | - 'offset' => 2, |
|
67 | - 'length' => 3, |
|
68 | - 'type' => 'italic', |
|
66 | + 'offset' => 2, |
|
67 | + 'length' => 3, |
|
68 | + 'type' => 'italic', |
|
69 | 69 | ], |
70 | 70 | [ |
71 | - 'offset' => 6, |
|
72 | - 'length' => 7, |
|
73 | - 'type' => 'code', |
|
71 | + 'offset' => 6, |
|
72 | + 'length' => 7, |
|
73 | + 'type' => 'code', |
|
74 | + ] |
|
74 | 75 | ] |
75 | - ] |
|
76 | - ]); |
|
77 | - } |
|
76 | + ]); |
|
77 | + } |
|
78 | 78 | |
79 | - public static function messageWithReply(): Message { |
|
80 | - return new Message([ |
|
81 | - 'message_id' => self::faker()->randomNumber(), |
|
82 | - 'from' => [ |
|
79 | + public static function messageWithReply(): Message { |
|
80 | + return new Message([ |
|
81 | + 'message_id' => self::faker()->randomNumber(), |
|
82 | + 'from' => [ |
|
83 | 83 | 'id' => self::faker()->randomNumber(), |
84 | 84 | 'is_bot' => false, |
85 | 85 | 'first_name' => self::faker()->firstName(), |
86 | 86 | 'last_name' => self::faker()->lastName(), |
87 | 87 | 'username' => self::faker()->userName(), |
88 | 88 | 'language_code' => self::faker()->languageCode(), |
89 | - ], |
|
90 | - 'chat' => [ |
|
89 | + ], |
|
90 | + 'chat' => [ |
|
91 | 91 | 'id' => self::faker()->randomNumber(), |
92 | 92 | 'first_name' => self::faker()->firstName(), |
93 | 93 | 'last_name' => self::faker()->lastName(), |
94 | 94 | 'username' => self::faker()->userName(), |
95 | 95 | 'type' => 'private', |
96 | - ], |
|
97 | - 'date' => self::faker()->unixTime(), |
|
98 | - 'text' => self::faker()->sentence(), |
|
99 | - 'reply_to_message' => [ |
|
96 | + ], |
|
97 | + 'date' => self::faker()->unixTime(), |
|
98 | + 'text' => self::faker()->sentence(), |
|
99 | + 'reply_to_message' => [ |
|
100 | 100 | 'message_id' => self::faker()->randomNumber(), |
101 | 101 | 'from' => [ |
102 | - 'id' => self::faker()->randomNumber(), |
|
103 | - 'is_bot' => false, |
|
104 | - 'first_name' => self::faker()->firstName(), |
|
105 | - 'last_name' => self::faker()->lastName(), |
|
106 | - 'username' => self::faker()->userName(), |
|
107 | - 'language_code' => self::faker()->languageCode(), |
|
102 | + 'id' => self::faker()->randomNumber(), |
|
103 | + 'is_bot' => false, |
|
104 | + 'first_name' => self::faker()->firstName(), |
|
105 | + 'last_name' => self::faker()->lastName(), |
|
106 | + 'username' => self::faker()->userName(), |
|
107 | + 'language_code' => self::faker()->languageCode(), |
|
108 | 108 | ], |
109 | 109 | 'chat' => [ |
110 | - 'id' => self::faker()->randomNumber(), |
|
111 | - 'first_name' => self::faker()->firstName(), |
|
112 | - 'last_name' => self::faker()->lastName(), |
|
113 | - 'username' => self::faker()->userName(), |
|
114 | - 'type' => 'private', |
|
110 | + 'id' => self::faker()->randomNumber(), |
|
111 | + 'first_name' => self::faker()->firstName(), |
|
112 | + 'last_name' => self::faker()->lastName(), |
|
113 | + 'username' => self::faker()->userName(), |
|
114 | + 'type' => 'private', |
|
115 | 115 | ], |
116 | 116 | 'date' => self::faker()->unixTime(), |
117 | 117 | 'text' => self::faker()->sentence(), |
118 | - ] |
|
119 | - ]); |
|
120 | - } |
|
118 | + ] |
|
119 | + ]); |
|
120 | + } |
|
121 | 121 | |
122 | 122 | } |
123 | 123 | \ No newline at end of file |
@@ -17,237 +17,237 @@ |
||
17 | 17 | */ |
18 | 18 | class UpdateHandler extends Telegram implements HandlerInterface { |
19 | 19 | |
20 | - use HandlerTrait; |
|
21 | - |
|
22 | - /** |
|
23 | - * @var ?Update |
|
24 | - */ |
|
25 | - protected ?Update $update; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var Plugin[] |
|
29 | - */ |
|
30 | - private array $plugins = []; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var bool |
|
34 | - */ |
|
35 | - private bool $active_spreader = false; |
|
36 | - |
|
37 | - /** |
|
38 | - * The default configuration of the webhook. |
|
39 | - * |
|
40 | - * @var array |
|
41 | - */ |
|
42 | - private array $config = [ |
|
43 | - 'autoload_env_file' => false, |
|
44 | - 'env_file_path' => null, |
|
45 | - ]; |
|
46 | - |
|
47 | - /** |
|
48 | - * Filter incoming updates. |
|
49 | - * |
|
50 | - * @var array |
|
51 | - */ |
|
52 | - private array $filterIncomingUpdates = []; |
|
53 | - |
|
54 | - /** |
|
55 | - * Webhook constructor. |
|
56 | - * |
|
57 | - * @param string $api_token The API key of the bot. Leave it blank for autoload from .env file. |
|
58 | - */ |
|
59 | - public function __construct(string $api_token = '') { |
|
60 | - parent::__construct($api_token); |
|
61 | - |
|
62 | - if (!Telegram::validateToken(self::getApiToken())) { |
|
63 | - throw new InvalidBotTokenException(); |
|
64 | - } |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * Resolve the request on single plugin. |
|
69 | - * |
|
70 | - * @param Plugin $plugin The plugin to work with |
|
71 | - * @param ?Update $update The custom to work with |
|
72 | - * @param array $config The configuration of the receiver |
|
73 | - * @return void |
|
74 | - */ |
|
75 | - public static function resolveOn(Plugin $plugin, Update $update = null, array $config = []): void { |
|
76 | - // TODO: Implement resolveOn() method. |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Add plugins to the receiver |
|
81 | - * |
|
82 | - * @param Plugin[]|array $plugins |
|
83 | - * @retrun void |
|
84 | - */ |
|
85 | - public function addPlugins(Plugin|array $plugins): UpdateHandler { |
|
86 | - if (is_object($plugins)) { |
|
87 | - $plugins = [$plugins]; |
|
88 | - } |
|
89 | - |
|
90 | - foreach ($plugins as $plugin) { |
|
91 | - if (!is_subclass_of($plugin, Plugin::class)) { |
|
20 | + use HandlerTrait; |
|
21 | + |
|
22 | + /** |
|
23 | + * @var ?Update |
|
24 | + */ |
|
25 | + protected ?Update $update; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var Plugin[] |
|
29 | + */ |
|
30 | + private array $plugins = []; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var bool |
|
34 | + */ |
|
35 | + private bool $active_spreader = false; |
|
36 | + |
|
37 | + /** |
|
38 | + * The default configuration of the webhook. |
|
39 | + * |
|
40 | + * @var array |
|
41 | + */ |
|
42 | + private array $config = [ |
|
43 | + 'autoload_env_file' => false, |
|
44 | + 'env_file_path' => null, |
|
45 | + ]; |
|
46 | + |
|
47 | + /** |
|
48 | + * Filter incoming updates. |
|
49 | + * |
|
50 | + * @var array |
|
51 | + */ |
|
52 | + private array $filterIncomingUpdates = []; |
|
53 | + |
|
54 | + /** |
|
55 | + * Webhook constructor. |
|
56 | + * |
|
57 | + * @param string $api_token The API key of the bot. Leave it blank for autoload from .env file. |
|
58 | + */ |
|
59 | + public function __construct(string $api_token = '') { |
|
60 | + parent::__construct($api_token); |
|
61 | + |
|
62 | + if (!Telegram::validateToken(self::getApiToken())) { |
|
63 | + throw new InvalidBotTokenException(); |
|
64 | + } |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * Resolve the request on single plugin. |
|
69 | + * |
|
70 | + * @param Plugin $plugin The plugin to work with |
|
71 | + * @param ?Update $update The custom to work with |
|
72 | + * @param array $config The configuration of the receiver |
|
73 | + * @return void |
|
74 | + */ |
|
75 | + public static function resolveOn(Plugin $plugin, Update $update = null, array $config = []): void { |
|
76 | + // TODO: Implement resolveOn() method. |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Add plugins to the receiver |
|
81 | + * |
|
82 | + * @param Plugin[]|array $plugins |
|
83 | + * @retrun void |
|
84 | + */ |
|
85 | + public function addPlugins(Plugin|array $plugins): UpdateHandler { |
|
86 | + if (is_object($plugins)) { |
|
87 | + $plugins = [$plugins]; |
|
88 | + } |
|
89 | + |
|
90 | + foreach ($plugins as $plugin) { |
|
91 | + if (!is_subclass_of($plugin, Plugin::class)) { |
|
92 | 92 | throw new \RuntimeException( |
93 | - sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
|
93 | + sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
|
94 | 94 | ); |
95 | - } |
|
95 | + } |
|
96 | 96 | |
97 | - $reflection = Toolkit::reflectionClass($plugin); |
|
98 | - $this->plugins[] = [ |
|
97 | + $reflection = Toolkit::reflectionClass($plugin); |
|
98 | + $this->plugins[] = [ |
|
99 | 99 | 'class' => $plugin, |
100 | 100 | 'initialized' => is_object($plugin), |
101 | - ]; |
|
102 | - } |
|
103 | - |
|
104 | - return $this; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Updates the filter for incoming updates. |
|
109 | - * |
|
110 | - * @param array $filter |
|
111 | - * @return void |
|
112 | - */ |
|
113 | - public function filterIncomingUpdates(array $filter): void { |
|
114 | - $this->filterIncomingUpdates = $filter; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Do not process updates that match the filter. |
|
119 | - * |
|
120 | - * @param Update $update |
|
121 | - * @return bool |
|
122 | - */ |
|
123 | - private function isFiltered(Update $update): bool { |
|
124 | - if (empty($this->filterIncomingUpdates)) { |
|
125 | - return false; |
|
126 | - } |
|
127 | - |
|
128 | - foreach ($this->filterIncomingUpdates as $type => $value) { |
|
129 | - if (is_int($type)) { |
|
101 | + ]; |
|
102 | + } |
|
103 | + |
|
104 | + return $this; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Updates the filter for incoming updates. |
|
109 | + * |
|
110 | + * @param array $filter |
|
111 | + * @return void |
|
112 | + */ |
|
113 | + public function filterIncomingUpdates(array $filter): void { |
|
114 | + $this->filterIncomingUpdates = $filter; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Do not process updates that match the filter. |
|
119 | + * |
|
120 | + * @param Update $update |
|
121 | + * @return bool |
|
122 | + */ |
|
123 | + private function isFiltered(Update $update): bool { |
|
124 | + if (empty($this->filterIncomingUpdates)) { |
|
125 | + return false; |
|
126 | + } |
|
127 | + |
|
128 | + foreach ($this->filterIncomingUpdates as $type => $value) { |
|
129 | + if (is_int($type)) { |
|
130 | 130 | if ($update->getUpdateType() === $value) { |
131 | - return true; |
|
131 | + return true; |
|
132 | 132 | } |
133 | - } elseif (is_string($type)) { |
|
133 | + } elseif (is_string($type)) { |
|
134 | 134 | if ($update->getUpdateType() === $type) { |
135 | - if (is_callable($value)) { |
|
136 | - return $value($update); |
|
137 | - } elseif (is_bool($value)) { |
|
138 | - return $value; |
|
139 | - } |
|
140 | - throw new \InvalidArgumentException('The value of the filter must be a callable or a boolean'); |
|
135 | + if (is_callable($value)) { |
|
136 | + return $value($update); |
|
137 | + } elseif (is_bool($value)) { |
|
138 | + return $value; |
|
139 | + } |
|
140 | + throw new \InvalidArgumentException('The value of the filter must be a callable or a boolean'); |
|
141 | + } |
|
141 | 142 | } |
142 | - } |
|
143 | - throw new \InvalidArgumentException('Invalid filter'); |
|
144 | - } |
|
145 | - |
|
146 | - return false; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Resolve the request. |
|
151 | - * |
|
152 | - * @param ?Update $update The custom to work with |
|
153 | - * @param array $config The configuration of the receiver |
|
154 | - * |
|
155 | - * @retrun void |
|
156 | - */ |
|
157 | - public function resolve(Update|null $update = null, array $config = []): void { |
|
158 | - $this->update = $update ?? Telegram::getUpdate(); |
|
159 | - |
|
160 | - if (empty($this->update)) { |
|
161 | - TelegramLog::error('The update is empty, the request is not processed'); |
|
162 | - return; |
|
163 | - } |
|
164 | - |
|
165 | - if ($this->isFiltered($this->update)) { |
|
166 | - TelegramLog::notice('The update is filtered, the request is not processed'); |
|
167 | - return; |
|
168 | - } |
|
169 | - |
|
170 | - if (!method_exists($this, '__process')) { |
|
171 | - throw new \RuntimeException('The method __process does not exist'); |
|
172 | - } |
|
173 | - |
|
174 | - if (is_array($config)) { |
|
175 | - $this->updateConfiguration($config); |
|
176 | - } |
|
177 | - |
|
178 | - putenv('TG_CURRENT_UPDATE=' . $this->update->getRawData(false)); |
|
179 | - |
|
180 | - $this->__process($this->update); |
|
181 | - $this->loadPlugins($this->plugins); |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * Update the configuration |
|
186 | - * |
|
187 | - * @param array $configuration |
|
188 | - * @return void |
|
189 | - */ |
|
190 | - public function updateConfiguration(array $configuration): void { |
|
191 | - $this->config = array_merge($this->config, $configuration); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Match the update with the given plugins |
|
196 | - * |
|
197 | - * @param Plugin[]|array $plugins |
|
198 | - * @return void |
|
199 | - */ |
|
200 | - private function loadPlugins(array $plugins): void { |
|
201 | - $update = $update ?? ($this->update ?? Telegram::getUpdate()); |
|
202 | - |
|
203 | - foreach ($plugins as $plugin) { |
|
204 | - if (!is_subclass_of($plugin['class'], Plugin::class)) { |
|
143 | + throw new \InvalidArgumentException('Invalid filter'); |
|
144 | + } |
|
145 | + |
|
146 | + return false; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Resolve the request. |
|
151 | + * |
|
152 | + * @param ?Update $update The custom to work with |
|
153 | + * @param array $config The configuration of the receiver |
|
154 | + * |
|
155 | + * @retrun void |
|
156 | + */ |
|
157 | + public function resolve(Update|null $update = null, array $config = []): void { |
|
158 | + $this->update = $update ?? Telegram::getUpdate(); |
|
159 | + |
|
160 | + if (empty($this->update)) { |
|
161 | + TelegramLog::error('The update is empty, the request is not processed'); |
|
162 | + return; |
|
163 | + } |
|
164 | + |
|
165 | + if ($this->isFiltered($this->update)) { |
|
166 | + TelegramLog::notice('The update is filtered, the request is not processed'); |
|
167 | + return; |
|
168 | + } |
|
169 | + |
|
170 | + if (!method_exists($this, '__process')) { |
|
171 | + throw new \RuntimeException('The method __process does not exist'); |
|
172 | + } |
|
173 | + |
|
174 | + if (is_array($config)) { |
|
175 | + $this->updateConfiguration($config); |
|
176 | + } |
|
177 | + |
|
178 | + putenv('TG_CURRENT_UPDATE=' . $this->update->getRawData(false)); |
|
179 | + |
|
180 | + $this->__process($this->update); |
|
181 | + $this->loadPlugins($this->plugins); |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * Update the configuration |
|
186 | + * |
|
187 | + * @param array $configuration |
|
188 | + * @return void |
|
189 | + */ |
|
190 | + public function updateConfiguration(array $configuration): void { |
|
191 | + $this->config = array_merge($this->config, $configuration); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Match the update with the given plugins |
|
196 | + * |
|
197 | + * @param Plugin[]|array $plugins |
|
198 | + * @return void |
|
199 | + */ |
|
200 | + private function loadPlugins(array $plugins): void { |
|
201 | + $update = $update ?? ($this->update ?? Telegram::getUpdate()); |
|
202 | + |
|
203 | + foreach ($plugins as $plugin) { |
|
204 | + if (!is_subclass_of($plugin['class'], Plugin::class)) { |
|
205 | 205 | throw new \InvalidArgumentException(sprintf( |
206 | - 'The plugin %s must be an instance of %s', |
|
207 | - get_class($plugin['class']), Plugin::class |
|
206 | + 'The plugin %s must be an instance of %s', |
|
207 | + get_class($plugin['class']), Plugin::class |
|
208 | 208 | )); |
209 | - } |
|
210 | - } |
|
209 | + } |
|
210 | + } |
|
211 | 211 | |
212 | - if (!$update instanceof Update) { |
|
213 | - throw new \InvalidArgumentException(sprintf( |
|
212 | + if (!$update instanceof Update) { |
|
213 | + throw new \InvalidArgumentException(sprintf( |
|
214 | 214 | 'The update must be an instance of %s. %s given', |
215 | 215 | Update::class, gettype($update) |
216 | - )); |
|
217 | - } |
|
218 | - |
|
219 | - $this->spreadUpdateWith($update, $plugins); |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * This function will get update and spread it to the plugins |
|
224 | - * |
|
225 | - * @param Update $update |
|
226 | - * @param array<Plugin> $plugins |
|
227 | - * @return void |
|
228 | - */ |
|
229 | - private function spreadUpdateWith(Update $update, array $plugins): void { |
|
230 | - $this->active_spreader = true; |
|
231 | - |
|
232 | - foreach ($plugins as $plugin) { |
|
233 | - if ($plugin['initialized'] === false) { |
|
216 | + )); |
|
217 | + } |
|
218 | + |
|
219 | + $this->spreadUpdateWith($update, $plugins); |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * This function will get update and spread it to the plugins |
|
224 | + * |
|
225 | + * @param Update $update |
|
226 | + * @param array<Plugin> $plugins |
|
227 | + * @return void |
|
228 | + */ |
|
229 | + private function spreadUpdateWith(Update $update, array $plugins): void { |
|
230 | + $this->active_spreader = true; |
|
231 | + |
|
232 | + foreach ($plugins as $plugin) { |
|
233 | + if ($plugin['initialized'] === false) { |
|
234 | 234 | $plugin['class'] = new $plugin['class'](); |
235 | - } |
|
236 | - |
|
237 | - $plugin['class']->__execute($this, $update); |
|
238 | - if ($this->active_spreader === false) break; |
|
239 | - } |
|
240 | - |
|
241 | - $this->active_spreader = false; |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * Stop the spreader process |
|
246 | - * |
|
247 | - * @return void |
|
248 | - */ |
|
249 | - public function stop(): void { |
|
250 | - $this->active_spreader = false; |
|
251 | - } |
|
235 | + } |
|
236 | + |
|
237 | + $plugin['class']->__execute($this, $update); |
|
238 | + if ($this->active_spreader === false) break; |
|
239 | + } |
|
240 | + |
|
241 | + $this->active_spreader = false; |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * Stop the spreader process |
|
246 | + * |
|
247 | + * @return void |
|
248 | + */ |
|
249 | + public function stop(): void { |
|
250 | + $this->active_spreader = false; |
|
251 | + } |
|
252 | 252 | |
253 | 253 | } |
254 | 254 | \ No newline at end of file |
@@ -235,7 +235,9 @@ |
||
235 | 235 | } |
236 | 236 | |
237 | 237 | $plugin['class']->__execute($this, $update); |
238 | - if ($this->active_spreader === false) break; |
|
238 | + if ($this->active_spreader === false) { |
|
239 | + break; |
|
240 | + } |
|
239 | 241 | } |
240 | 242 | |
241 | 243 | $this->active_spreader = false; |
@@ -16,98 +16,98 @@ |
||
16 | 16 | */ |
17 | 17 | trait PluginTrait { |
18 | 18 | |
19 | - /** |
|
20 | - * @var UpdateHandler |
|
21 | - */ |
|
22 | - protected UpdateHandler $hook; |
|
23 | - |
|
24 | - /** |
|
25 | - * This property is used to kill the plugin when you yield a Response object. |
|
26 | - * |
|
27 | - * @var bool |
|
28 | - */ |
|
29 | - protected bool $KILL_ON_YIELD = true; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var \Generator |
|
33 | - */ |
|
34 | - private \Generator $returns; |
|
35 | - |
|
36 | - /** |
|
37 | - * Execute the plugin. |
|
38 | - * |
|
39 | - * @param UpdateHandler $receiver |
|
40 | - * @param Update $update |
|
41 | - * @return void |
|
42 | - */ |
|
43 | - public function __execute(UpdateHandler $receiver, Update $update): void { |
|
44 | - $this->hook = $receiver; |
|
45 | - |
|
46 | - if (method_exists($this, '__process')) { |
|
47 | - $this->__process($update); |
|
48 | - } |
|
49 | - |
|
50 | - if (method_exists($this, 'onReceivedUpdate')) { |
|
51 | - $return = $this->onUpdate($update); |
|
52 | - $this->__checkExit($return); |
|
53 | - } |
|
54 | - |
|
55 | - $type = $this->__identify($update); |
|
56 | - if (method_exists($this, ($method = 'on' . $type)) && $type !== null) { |
|
57 | - $this->__checkExit($this->__callEvent($method, $update)); |
|
58 | - } |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Check for the exit of the plugin. |
|
63 | - * |
|
64 | - * @param \Generator $return |
|
65 | - * @return void |
|
66 | - */ |
|
67 | - private function __checkExit(\Generator $return): void { |
|
68 | - if ($return->valid()) { |
|
69 | - if ($return->current() !== null && $this->KILL_ON_YIELD === true) { |
|
19 | + /** |
|
20 | + * @var UpdateHandler |
|
21 | + */ |
|
22 | + protected UpdateHandler $hook; |
|
23 | + |
|
24 | + /** |
|
25 | + * This property is used to kill the plugin when you yield a Response object. |
|
26 | + * |
|
27 | + * @var bool |
|
28 | + */ |
|
29 | + protected bool $KILL_ON_YIELD = true; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var \Generator |
|
33 | + */ |
|
34 | + private \Generator $returns; |
|
35 | + |
|
36 | + /** |
|
37 | + * Execute the plugin. |
|
38 | + * |
|
39 | + * @param UpdateHandler $receiver |
|
40 | + * @param Update $update |
|
41 | + * @return void |
|
42 | + */ |
|
43 | + public function __execute(UpdateHandler $receiver, Update $update): void { |
|
44 | + $this->hook = $receiver; |
|
45 | + |
|
46 | + if (method_exists($this, '__process')) { |
|
47 | + $this->__process($update); |
|
48 | + } |
|
49 | + |
|
50 | + if (method_exists($this, 'onReceivedUpdate')) { |
|
51 | + $return = $this->onUpdate($update); |
|
52 | + $this->__checkExit($return); |
|
53 | + } |
|
54 | + |
|
55 | + $type = $this->__identify($update); |
|
56 | + if (method_exists($this, ($method = 'on' . $type)) && $type !== null) { |
|
57 | + $this->__checkExit($this->__callEvent($method, $update)); |
|
58 | + } |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Check for the exit of the plugin. |
|
63 | + * |
|
64 | + * @param \Generator $return |
|
65 | + * @return void |
|
66 | + */ |
|
67 | + private function __checkExit(\Generator $return): void { |
|
68 | + if ($return->valid()) { |
|
69 | + if ($return->current() !== null && $this->KILL_ON_YIELD === true) { |
|
70 | 70 | if ($return->current() instanceof Response) { |
71 | - $this->stop(); |
|
71 | + $this->stop(); |
|
72 | 72 | } |
73 | - } |
|
74 | - } |
|
75 | - |
|
76 | - if ($return->valid()) { |
|
77 | - $return->next(); |
|
78 | - $this->__checkExit($return); |
|
79 | - } |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Identify the update type. e.g. Message, EditedMessage, etc. |
|
84 | - * |
|
85 | - * @param Update $update |
|
86 | - * @return string|null |
|
87 | - */ |
|
88 | - private function __identify(Update $update): ?string { |
|
89 | - $type = $update->getUpdateType(); |
|
90 | - |
|
91 | - if ($type === null) { |
|
92 | - return null; |
|
93 | - } |
|
94 | - |
|
95 | - return str_replace('_', '', ucwords($type, '_')); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Pass data to the method. |
|
100 | - * |
|
101 | - * @param string $method The method name. |
|
102 | - * @param Update $update The update object. |
|
103 | - * @return \Generator |
|
104 | - */ |
|
105 | - private function __callEvent(string $method, Update $update): \Generator { |
|
106 | - $upperName = 'get' . ucfirst(substr($method, 2)); |
|
107 | - return match ($method) { |
|
108 | - 'onWebAppData' => $this->onWebAppData($update->getWebAppData()), |
|
109 | - default => $this->$method($update->getUpdateId(), $update->$upperName()), |
|
110 | - }; |
|
111 | - } |
|
73 | + } |
|
74 | + } |
|
75 | + |
|
76 | + if ($return->valid()) { |
|
77 | + $return->next(); |
|
78 | + $this->__checkExit($return); |
|
79 | + } |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Identify the update type. e.g. Message, EditedMessage, etc. |
|
84 | + * |
|
85 | + * @param Update $update |
|
86 | + * @return string|null |
|
87 | + */ |
|
88 | + private function __identify(Update $update): ?string { |
|
89 | + $type = $update->getUpdateType(); |
|
90 | + |
|
91 | + if ($type === null) { |
|
92 | + return null; |
|
93 | + } |
|
94 | + |
|
95 | + return str_replace('_', '', ucwords($type, '_')); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Pass data to the method. |
|
100 | + * |
|
101 | + * @param string $method The method name. |
|
102 | + * @param Update $update The update object. |
|
103 | + * @return \Generator |
|
104 | + */ |
|
105 | + private function __callEvent(string $method, Update $update): \Generator { |
|
106 | + $upperName = 'get' . ucfirst(substr($method, 2)); |
|
107 | + return match ($method) { |
|
108 | + 'onWebAppData' => $this->onWebAppData($update->getWebAppData()), |
|
109 | + default => $this->$method($update->getUpdateId(), $update->$upperName()), |
|
110 | + }; |
|
111 | + } |
|
112 | 112 | |
113 | 113 | } |
114 | 114 | \ No newline at end of file |