Skip to content

e-vyatkin/event-snoozer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status SensioLabsInsight MIT Licence Code Climate

EventSnoozer

Current library allows You to store events and dispatch them later.

Usage

  • Implement EventStorageInterface or use MemoryEventStorage. I suggest to use database or cache for storing events. For example, I'll use MemoryEventStorage.
    It will store snoozed events until script stop running.

  • Create event class that you want to save. You could extend EventDispatcher Event class, but if you want to specify additional data and/or priority, extend RealEvent class.

<?php
  
namespace MyApp;
  
use EventSnoozer\RealEvent;  
  
class MyEvent extends RealEvent
{
    const NAME = 'my.event';
}
  • Instantiate EventSnoozer class and use it for saving events.
<?php
  
namespace MyApp;
  
use EventSnoozer\EventStorage\MemoryEventStorage;
use EventSnoozer\EventSnoozer;
use MyApp\MyEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
  
$eventStorage = new MemoryEventStorage();
$eventDispatcher = new EventDispatcher();
// Add listeners/subscribers for your events
  
$eventSnoozer = new EventSnoozer($eventDispatcher, $eventStorage);
  
$event = new MyEvent();
$event->setPriority(10)
    ->setAdditionalData(array('data' => 'value'));
  
$eventSnoozer->snoozeEvent(MyEvent::NAME, $event, '+10 min');
  • Use EventSnoozer for dispatching saved events.
    MemoryEventStorage would select events ordered by priority and runtime.
<?php
namespace MyApp;
  
use EventSnoozer\EventStorage\MemoryEventStorage;
use EventSnoozer\EventSnoozer;
use Symfony\Component\EventDispatcher\EventDispatcher;
  
$eventStorage = new MemoryEventStorage();
$eventDispatcher = new EventDispatcher();
// Add listeners/subscribers for your events
  
$eventSnoozer = new EventSnoozer($eventDispatcher, $eventStorage);
  
$eventSnoozer->dispatchSnoozedEvent(); // For single event
$eventSnoozer->dispatchMultipleSnoozedEvents(5); // For multiple events

I suggest to use symfony/console for creating console commands and run it in background by cronjob.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages