Skip to content

Commit

Permalink
Use events to notify about updates (#175)
Browse files Browse the repository at this point in the history
* Use events to notify about updates

* Add ability to avoid event emitting

* Rename update event

* Exclude Event part from event name
  • Loading branch information
Artiom Mocrenco authored and irazasyed committed Apr 30, 2016
1 parent 21e2143 commit 2bd91c8
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 8 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"require": {
"php": ">=5.5.0",
"guzzlehttp/guzzle": "~6.0",
"illuminate/support": "~5.0"
"illuminate/support": "~5.0",
"league/event": "^2.1"
},
"require-dev": {
"phpunit/phpunit": "^4.8 || ^5.0",
Expand Down
30 changes: 23 additions & 7 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

use Illuminate\Contracts\Container\Container;
use Telegram\Bot\Commands\CommandBus;
use Telegram\Bot\Events\EmitsEvents;
use Telegram\Bot\Events\UpdateWasReceived;
use Telegram\Bot\Exceptions\TelegramSDKException;
use Telegram\Bot\FileUpload\InputFile;
use Telegram\Bot\HttpClients\GuzzleHttpClient;
use Telegram\Bot\HttpClients\HttpClientInterface;
use Telegram\Bot\Objects\File;
use Telegram\Bot\Objects\Message;
Expand All @@ -23,6 +24,8 @@
*/
class Api
{
use EmitsEvents;

/**
* @var string Version number of the Telegram Bot PHP SDK.
*/
Expand Down Expand Up @@ -958,11 +961,17 @@ public function setWebhook(array $params)
*
* @return Update
*/
public function getWebhookUpdates()
public function getWebhookUpdates($emitUpdateWasReceivedEvent = true)
{
$body = json_decode(file_get_contents('php://input'), true);

return new Update($body);
$update = new Update($body);

if ($emitUpdateWasReceivedEvent) {
$this->emitEvent(new UpdateWasReceived($update));
}

return $update;
}

/**
Expand Down Expand Up @@ -991,22 +1000,29 @@ public function removeWebhook()
* @link https://core.telegram.org/bots/api#getupdates
*
* @param array $params
*
* @param bool $emitUpdateWasReceivedEvents
* @var int|null $params ['offset']
* @var int|null $params ['limit']
* @var int|null $params ['timeout']
*
* @return Update[]
*/
public function getUpdates(array $params = [])
public function getUpdates(array $params = [], $emitUpdateWasReceivedEvents = true)
{
$response = $this->post('getUpdates', $params);
$updates = $response->getDecodedBody();

/** @var Update[] $data */
$data = [];
if (isset($updates['result'])) {
foreach ($updates['result'] as $update) {
$data[] = new Update($update);
foreach ($updates['result'] as $body) {
$update = new Update($body);

if ($emitUpdateWasReceivedEvents) {
$this->emitEvent(new UpdateWasReceived($update));
}

$data[] = $update;
}
}

Expand Down
72 changes: 72 additions & 0 deletions src/Events/EmitsEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Telegram\Bot\Events;

use League\Event\Emitter;
use League\Event\EventInterface;

trait EmitsEvents
{
/**
* @var Emitter
*/
private $eventEmitter;

/**
* @param EventInterface|string $event
* @throws \InvalidArgumentException
* @return bool true if emitted, false otherwise
*/
private function emitEvent($event)
{
if (is_null($this->eventEmitter)) {
return false;
}

if (!is_string($event) && !$event instanceof EventInterface) {
throw new \InvalidArgumentException('Event must be either be of type "string" or instance of League\Event\EventInterface');
}

$this->eventEmitter->emit($event);

return true;
}

/**
* @param EventInterface[]|string[] $events
* @throws \InvalidArgumentException
* @return bool true if all emitted, false otherwise
*/
private function emitBatchOfEvents(array $events)
{
if (is_null($this->eventEmitter)) {
return false;
}

foreach ($events as $e) {
if (!is_string($e) && !$e instanceof EventInterface) {
throw new \InvalidArgumentException('Event must be either be of type "string" or instance of League\Event\EventInterface');
}
}

$this->emitBatchOfEvents($events);

return true;
}

/**
* @return Emitter
*/
public function getEventEmitter()
{
return $this->eventEmitter;
}

/**
* @param Emitter $eventEmitter
*/
public function setEventEmitter($eventEmitter)
{
$this->eventEmitter = $eventEmitter;
}
}
31 changes: 31 additions & 0 deletions src/Events/UpdateWasReceived.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Telegram\Bot\Events;

use League\Event\AbstractEvent;
use Telegram\Bot\Objects\Update;

class UpdateWasReceived extends AbstractEvent
{
/**
* @var Update
*/
private $update;

/**
* UpdateWasReceived constructor.
* @param Update $update
*/
public function __construct(Update $update)
{
$this->update = $update;
}

/**
* @return Update
*/
public function getUpdate()
{
return $this->update;
}
}

0 comments on commit 2bd91c8

Please sign in to comment.