zhem4ag/event-snoozer

View on GitHub
README.md

Summary

Maintainability
Test Coverage
[![Build Status](https://travis-ci.org/zhem4ag/event-snoozer.svg?branch=master)](https://travis-ci.org/zhem4ag/event-snoozer)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/80000129-230e-47ae-aeca-efc0f8f3a966/mini.png)](https://insight.sensiolabs.com/projects/80000129-230e-47ae-aeca-efc0f8f3a966)
[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php)
[![Code Climate](https://codeclimate.com/github/zhem4ag/event-snoozer/badges/gpa.svg)](https://codeclimate.com/github/zhem4ag/event-snoozer)

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
<?php
  
namespace MyApp;
  
use EventSnoozer\RealEvent;  
  
class MyEvent extends RealEvent
{
    const NAME = 'my.event';
}
```

* Instantiate EventSnoozer class and use it for saving events.

```php
<?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
<?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.