Passed
Push — master ( 584dd8...142ac3 )
by Shahrad
02:17
created
src/lib/WebhookHandler.php 1 patch
Indentation   +216 added lines, -216 removed lines patch added patch discarded remove patch
@@ -17,221 +17,221 @@
 block discarded – undo
17 17
 abstract class WebhookHandler extends Telegram
18 18
 {
19 19
 
20
-	/**
21
-	 * @var ?Update
22
-	 */
23
-	protected ?Update $update;
24
-
25
-	/**
26
-	 * @var array<Plugin>
27
-	 */
28
-	private array $plugins = [];
29
-
30
-	/**
31
-	 * @var bool
32
-	 */
33
-	private bool $isActive = false;
34
-
35
-	/**
36
-	 * The default configuration of the webhook.
37
-	 *
38
-	 * @var array
39
-	 */
40
-	private array $config = [
41
-		'autoload_env_file' => false,
42
-		'env_file_path' => null,
43
-	];
44
-
45
-	/**
46
-	 * Webhook constructor.
47
-	 *
48
-	 * @param string $api_key The API key of the bot. Leave it blank for autoload from .env file.
49
-	 */
50
-	public function __construct(string $api_key = '')
51
-	{
52
-		parent::__construct($api_key);
53
-
54
-		if (!Telegram::validateToken(self::getApiKey())) {
55
-			throw new InvalidBotTokenException();
56
-		}
57
-	}
58
-
59
-	/**
60
-	 * Initialize the receiver.
61
-	 *
62
-	 * @return void
63
-	 */
64
-	public function init(): void
65
-	{
66
-		$this->config['env_file_path'] = $_SERVER['DOCUMENT_ROOT'] . '/.env';
67
-	}
68
-
69
-	/**
70
-	 * Add a plugin to the receiver
71
-	 *
72
-	 * @param array<Plugin> $plugins
73
-	 */
74
-	public function addPlugin(array $plugins): void
75
-	{
76
-		foreach ($plugins as $plugin) {
77
-			if (!is_subclass_of($plugin, Plugin::class)) {
78
-				throw new \RuntimeException(
79
-					sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class)
80
-				);
81
-			}
82
-			$this->plugins[] = $plugin;
83
-		}
84
-	}
85
-
86
-	/**
87
-	 * Match the update with the given plugins
88
-	 *
89
-	 * @param array<Plugin> $plugins
90
-	 * @param ?Update $update The update to match
91
-	 * @return void
92
-	 */
93
-	public function loadPluginsWith(array $plugins, Update $update = null): void
94
-	{
95
-		$update = $update ?? ($this->update ?? Telegram::getUpdate());
96
-
97
-		foreach ($plugins as $plugin) {
98
-			if (!is_subclass_of($plugin, Plugin::class)) {
99
-				throw new \InvalidArgumentException(
100
-					sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class)
101
-				);
102
-			}
103
-		}
104
-
105
-		if ($update instanceof Update) {
106
-			$this->spreadUpdateWith($update, $plugins);
107
-		}
108
-	}
109
-
110
-	/**
111
-	 * Match the message to the plugins
112
-	 *
113
-	 * @param ?Update $update The update to match
114
-	 * @return void
115
-	 */
116
-	public function loadPlugins(Update $update = null): void
117
-	{
118
-		$update = $update ?? ($this->update ?? Telegram::getUpdate());
119
-		$this->loadPluginsWith($this->plugins, $update);
120
-	}
121
-
122
-	/**
123
-	 * Load the environment's
124
-	 *
125
-	 * @param string $path
126
-	 * @retrun void
127
-	 */
128
-	public function loadFileOfEnv(string $path): void
129
-	{
130
-		DotEnv::load($path);
131
-	}
132
-
133
-	/**
134
-	 * Update the configuration
135
-	 *
136
-	 * @param array $configuration
137
-	 * @return void
138
-	 */
139
-	public function updateConfiguration(array $configuration): void
140
-	{
141
-		$this->config = array_merge($this->config, $configuration);
142
-	}
143
-
144
-	/**
145
-	 * Resolve the request.
146
-	 *
147
-	 * @param ?Update $update The custom to work with
148
-	 * @param array $config The configuration of the receiver
149
-	 *
150
-	 * @retrun void
151
-	 */
152
-	public function resolve(Update $update = null, array $config = []): void
153
-	{
154
-		$method = '__process';
155
-		if (!method_exists($this, $method)) {
156
-			throw new \RuntimeException(sprintf('The method %s does not exist', $method));
157
-		}
158
-
159
-		if (is_array($config)) $this->updateConfiguration($config);
160
-
161
-		if (!empty($update)) $this->update = $update;
162
-		else $this->update = Telegram::getUpdate() !== false ? Telegram::getUpdate() : null;
163
-
164
-		if (empty($this->update)) {
165
-			TelegramLog::error(
166
-				'The update is empty, the request is not processed'
167
-			);
168
-			return;
169
-		}
170
-
171
-		putenv('TG_CURRENT_UPDATE=' . $this->update->getRawData(false));
172
-
173
-		$this->$method($this->update);
174
-	}
175
-
176
-	/**
177
-	 * This function will get update and spread it to the plugins
178
-	 *
179
-	 * @param Update $update
180
-	 * @param array<Plugin> $plugins
181
-	 * @return void
182
-	 */
183
-	private function spreadUpdateWith(Update $update, array $plugins): void
184
-	{
185
-		$this->isActive = true;
186
-
187
-		foreach ($plugins as $plugin) {
188
-			/** @var Plugin $plugin */
189
-			(new $plugin())->__execute($this, $update);
190
-			if ($this->isActive === false) break;
191
-		}
192
-
193
-		$this->isActive = false;
194
-	}
195
-
196
-	/**
197
-	 * Kill the speeding process
198
-	 *
199
-	 * @return void
200
-	 */
201
-	public function kill(): void
202
-	{
203
-		$this->isActive = false;
204
-	}
205
-
206
-	/**
207
-	 * Use this method on the webhook class to get the updates
208
-	 *
209
-	 * @param Update $update
210
-	 * @return void
211
-	 */
212
-	abstract public function __process(Update $update): void;
213
-
214
-	/**
215
-	 * put CrossData into the plugins
216
-	 *
217
-	 * @param string $key
218
-	 * @param mixed $value
219
-	 * @return void
220
-	 */
221
-	public function putCrossData(string $key, mixed $value): void
222
-	{
223
-		CrossData::put($key, $value);
224
-	}
225
-
226
-	/**
227
-	 * get CrossData from the plugins
228
-	 *
229
-	 * @param string $key
230
-	 * @return string|array|bool|null
231
-	 */
232
-	public function getCrossData(string $key): string|array|bool|null
233
-	{
234
-		return CrossData::get($key);
235
-	}
20
+    /**
21
+     * @var ?Update
22
+     */
23
+    protected ?Update $update;
24
+
25
+    /**
26
+     * @var array<Plugin>
27
+     */
28
+    private array $plugins = [];
29
+
30
+    /**
31
+     * @var bool
32
+     */
33
+    private bool $isActive = false;
34
+
35
+    /**
36
+     * The default configuration of the webhook.
37
+     *
38
+     * @var array
39
+     */
40
+    private array $config = [
41
+        'autoload_env_file' => false,
42
+        'env_file_path' => null,
43
+    ];
44
+
45
+    /**
46
+     * Webhook constructor.
47
+     *
48
+     * @param string $api_key The API key of the bot. Leave it blank for autoload from .env file.
49
+     */
50
+    public function __construct(string $api_key = '')
51
+    {
52
+        parent::__construct($api_key);
53
+
54
+        if (!Telegram::validateToken(self::getApiKey())) {
55
+            throw new InvalidBotTokenException();
56
+        }
57
+    }
58
+
59
+    /**
60
+     * Initialize the receiver.
61
+     *
62
+     * @return void
63
+     */
64
+    public function init(): void
65
+    {
66
+        $this->config['env_file_path'] = $_SERVER['DOCUMENT_ROOT'] . '/.env';
67
+    }
68
+
69
+    /**
70
+     * Add a plugin to the receiver
71
+     *
72
+     * @param array<Plugin> $plugins
73
+     */
74
+    public function addPlugin(array $plugins): void
75
+    {
76
+        foreach ($plugins as $plugin) {
77
+            if (!is_subclass_of($plugin, Plugin::class)) {
78
+                throw new \RuntimeException(
79
+                    sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class)
80
+                );
81
+            }
82
+            $this->plugins[] = $plugin;
83
+        }
84
+    }
85
+
86
+    /**
87
+     * Match the update with the given plugins
88
+     *
89
+     * @param array<Plugin> $plugins
90
+     * @param ?Update $update The update to match
91
+     * @return void
92
+     */
93
+    public function loadPluginsWith(array $plugins, Update $update = null): void
94
+    {
95
+        $update = $update ?? ($this->update ?? Telegram::getUpdate());
96
+
97
+        foreach ($plugins as $plugin) {
98
+            if (!is_subclass_of($plugin, Plugin::class)) {
99
+                throw new \InvalidArgumentException(
100
+                    sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class)
101
+                );
102
+            }
103
+        }
104
+
105
+        if ($update instanceof Update) {
106
+            $this->spreadUpdateWith($update, $plugins);
107
+        }
108
+    }
109
+
110
+    /**
111
+     * Match the message to the plugins
112
+     *
113
+     * @param ?Update $update The update to match
114
+     * @return void
115
+     */
116
+    public function loadPlugins(Update $update = null): void
117
+    {
118
+        $update = $update ?? ($this->update ?? Telegram::getUpdate());
119
+        $this->loadPluginsWith($this->plugins, $update);
120
+    }
121
+
122
+    /**
123
+     * Load the environment's
124
+     *
125
+     * @param string $path
126
+     * @retrun void
127
+     */
128
+    public function loadFileOfEnv(string $path): void
129
+    {
130
+        DotEnv::load($path);
131
+    }
132
+
133
+    /**
134
+     * Update the configuration
135
+     *
136
+     * @param array $configuration
137
+     * @return void
138
+     */
139
+    public function updateConfiguration(array $configuration): void
140
+    {
141
+        $this->config = array_merge($this->config, $configuration);
142
+    }
143
+
144
+    /**
145
+     * Resolve the request.
146
+     *
147
+     * @param ?Update $update The custom to work with
148
+     * @param array $config The configuration of the receiver
149
+     *
150
+     * @retrun void
151
+     */
152
+    public function resolve(Update $update = null, array $config = []): void
153
+    {
154
+        $method = '__process';
155
+        if (!method_exists($this, $method)) {
156
+            throw new \RuntimeException(sprintf('The method %s does not exist', $method));
157
+        }
158
+
159
+        if (is_array($config)) $this->updateConfiguration($config);
160
+
161
+        if (!empty($update)) $this->update = $update;
162
+        else $this->update = Telegram::getUpdate() !== false ? Telegram::getUpdate() : null;
163
+
164
+        if (empty($this->update)) {
165
+            TelegramLog::error(
166
+                'The update is empty, the request is not processed'
167
+            );
168
+            return;
169
+        }
170
+
171
+        putenv('TG_CURRENT_UPDATE=' . $this->update->getRawData(false));
172
+
173
+        $this->$method($this->update);
174
+    }
175
+
176
+    /**
177
+     * This function will get update and spread it to the plugins
178
+     *
179
+     * @param Update $update
180
+     * @param array<Plugin> $plugins
181
+     * @return void
182
+     */
183
+    private function spreadUpdateWith(Update $update, array $plugins): void
184
+    {
185
+        $this->isActive = true;
186
+
187
+        foreach ($plugins as $plugin) {
188
+            /** @var Plugin $plugin */
189
+            (new $plugin())->__execute($this, $update);
190
+            if ($this->isActive === false) break;
191
+        }
192
+
193
+        $this->isActive = false;
194
+    }
195
+
196
+    /**
197
+     * Kill the speeding process
198
+     *
199
+     * @return void
200
+     */
201
+    public function kill(): void
202
+    {
203
+        $this->isActive = false;
204
+    }
205
+
206
+    /**
207
+     * Use this method on the webhook class to get the updates
208
+     *
209
+     * @param Update $update
210
+     * @return void
211
+     */
212
+    abstract public function __process(Update $update): void;
213
+
214
+    /**
215
+     * put CrossData into the plugins
216
+     *
217
+     * @param string $key
218
+     * @param mixed $value
219
+     * @return void
220
+     */
221
+    public function putCrossData(string $key, mixed $value): void
222
+    {
223
+        CrossData::put($key, $value);
224
+    }
225
+
226
+    /**
227
+     * get CrossData from the plugins
228
+     *
229
+     * @param string $key
230
+     * @return string|array|bool|null
231
+     */
232
+    public function getCrossData(string $key): string|array|bool|null
233
+    {
234
+        return CrossData::get($key);
235
+    }
236 236
 
237 237
 }
238 238
\ No newline at end of file
Please login to merge, or discard this patch.