-
Notifications
You must be signed in to change notification settings - Fork 660
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use events to notify about updates (#175)
* Use events to notify about updates * Add ability to avoid event emitting * Rename update event * Exclude Event part from event name
- Loading branch information
Showing
4 changed files
with
128 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |