TelegramBot /
Api
| 1 | <?php |
||
| 2 | |||
| 3 | namespace TelegramBot\Api\Events; |
||
| 4 | |||
| 5 | use Closure; |
||
| 6 | use ReflectionFunction; |
||
| 7 | use TelegramBot\Api\Botan; |
||
| 8 | use TelegramBot\Api\Types\Update; |
||
| 9 | |||
| 10 | class EventCollection |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Array of events. |
||
| 14 | * |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | protected $events; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Botan tracker |
||
| 21 | * |
||
| 22 | * @var \TelegramBot\Api\Botan|null |
||
| 23 | */ |
||
| 24 | protected $tracker; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * EventCollection constructor. |
||
| 28 | * |
||
| 29 | * @param string $trackerToken |
||
| 30 | */ |
||
| 31 | 13 | public function __construct($trackerToken = null) |
|
| 32 | { |
||
| 33 | 13 | $this->events = []; |
|
| 34 | 2 | if ($trackerToken) { |
|
| 35 | 2 | @trigger_error(sprintf('Passing $trackerToken to %s is deprecated', self::class), \E_USER_DEPRECATED); |
|
| 36 | 13 | $this->tracker = new Botan($trackerToken); |
|
|
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Add new event to collection |
||
| 42 | * |
||
| 43 | * @param Closure $event |
||
| 44 | * @param Closure|null $checker |
||
| 45 | * |
||
| 46 | * @return \TelegramBot\Api\Events\EventCollection |
||
| 47 | 4 | */ |
|
| 48 | public function add(Closure $event, $checker = null) |
||
| 49 | 4 | { |
|
| 50 | 1 | $this->events[] = !is_null($checker) ? new Event($event, $checker) |
|
| 51 | 1 | : new Event($event, function () {}); |
|
| 52 | |||
| 53 | 4 | return $this; |
|
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @return void |
||
| 58 | */ |
||
| 59 | 2 | public function handle(Update $update) |
|
| 60 | { |
||
| 61 | 2 | foreach ($this->events as $event) { |
|
| 62 | /* @var \TelegramBot\Api\Events\Event $event */ |
||
| 63 | 2 | if ($event->executeChecker($update) === true) { |
|
| 64 | 2 | if ($event->executeAction($update) === false) { |
|
| 65 | 1 | if ($this->tracker && ($message = $update->getMessage())) { |
|
| 66 | 1 | $checker = new ReflectionFunction($event->getChecker()); |
|
| 67 | 1 | $this->tracker->track($message, $checker->getStaticVariables()['name']); |
|
| 68 | 1 | } |
|
| 69 | 1 | break; |
|
| 70 | } |
||
| 71 | 1 | } |
|
| 72 | 2 | } |
|
| 73 | 2 | } |
|
| 74 | } |
||
| 75 |