Passed
Push — master ( 5c7744...96b75b )
by Shahrad
10:30
created

Plugin   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 33
dl 0
loc 112
rs 10
c 2
b 1
f 0
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __identify() 0 9 2
A __checkExit() 0 11 5
A __execute() 0 13 4
A kill() 0 3 1
1
<?php
2
3
namespace TelegramBot;
4
5
use TelegramBot\Entities\Update;
6
7
/**
8
 * Class Plugin
9
 *
10
 * @link    https://github.com/telegram-bot-php/core
11
 * @author  Shahrad Elahi (https://github.com/shahradelahi)
12
 * @license https://github.com/telegram-bot-php/core/blob/master/LICENSE (MIT License)
13
 */
14
abstract class Plugin
15
{
16
17
	/**
18
	 * The Update types
19
	 *
20
	 * @var array
21
	 */
22
	protected array $update_types = [
23
		'message',
24
		'edited_message',
25
		'channel_post',
26
		'edited_channel_post',
27
		'inline_query',
28
		'chosen_inline_result',
29
		'callback_query',
30
		'shipping_query',
31
		'pre_checkout_query',
32
	];
33
34
	/**
35
	 * @var WebhookHandler
36
	 */
37
	protected WebhookHandler $hook;
38
39
	/**
40
	 * @var \Generator
41
	 */
42
	protected \Generator $returns;
43
44
	/**
45
	 * @var bool
46
	 */
47
	protected bool $kill_on_yield = false;
48
49
	/**
50
	 * Check for the exit of the plugin.
51
	 *
52
	 * @param \Generator $return
53
	 * @return void
54
	 */
55
	public function __checkExit(\Generator $return): void
56
	{
57
		if ($return->valid()) {
58
			if ($return->current() !== null && $this->kill_on_yield === true) {
59
				$this->kill();
60
			}
61
		}
62
63
		if ($return->valid()) {
64
			$return->next();
65
			$this->__checkExit($return);
66
		}
67
	}
68
69
	/**
70
	 * Identify the update type. e.g. Message, EditedMessage, etc.
71
	 *
72
	 * @param Update $update
73
	 * @return string|null
74
	 */
75
	public function __identify(Update $update): ?string
76
	{
77
		$type = $update->getUpdateType();
78
79
		if ($type === null) {
80
			return null;
81
		}
82
83
		return str_replace('_', '', ucwords($type, '_'));
84
	}
85
86
87
	/**
88
	 * Execute the plugin.
89
	 *
90
	 * @param WebhookHandler $receiver
91
	 * @param Update $update
92
	 * @return void
93
	 */
94
	public function __execute(WebhookHandler $receiver, Update $update): void
95
	{
96
		$this->hook = $receiver;
97
98
		if (method_exists($this, 'onReceivedUpdate')) {
99
			$return = $this->onReceivedUpdate($update);
100
			$this->__checkExit($return);
101
		}
102
103
		$type = $this->__identify($update);
104
		if (method_exists($this, ($method = 'on' . $type)) && $type !== null) {
105
			$return = $this->$method($update);
106
			$this->__checkExit($return);
107
		}
108
	}
109
110
	/**
111
	 * Identify the update type and if method of the type is exists, execute it.
112
	 *
113
	 * @param Update $update
114
	 * @return \Generator
115
	 */
116
	abstract protected function onReceivedUpdate(Update $update): \Generator;
117
118
	/**
119
	 * Kill the plugin.
120
	 *
121
	 * @return void
122
	 */
123
	public function kill(): void
124
	{
125
		$this->hook->kill();
126
	}
127
128
}