Skip to content

Instantly share code, notes, and snippets.

@JeroenDeDauw
Last active August 21, 2018 09:42
Show Gist options
  • Save JeroenDeDauw/9a1081ea47440141879c3f9fb60c2229 to your computer and use it in GitHub Desktop.
Save JeroenDeDauw/9a1081ea47440141879c3f9fb60c2229 to your computer and use it in GitHub Desktop.
<?php
class DonationEvents {
private $donationCreatedCallbacks = [];
private $lolCatAddedCallbacks = [];
public function onDonationCreated( callable $callback ) {
$this->donationCreatedCallbacks[] = $callback;
}
public function onLolCatAdded( callable $callback ) {
$this->lolCatAddedCallbacks[] = $callback;
}
public function donationCreated( DonationCreatedEvent $createdEvent ) {
foreach ( $this->donationCreatedCallbacks as $callback ) {
$callback( $createdEvent );
}
}
public function lolCatAdded() {
foreach ( $this->lolCatAddedCallbacks as $callback ) {
$callback();
}
}
}
class DonationCreatedEvent {
public function getDonationId(): int {
}
}
class EventRegisterer {
/**
* Called during application initialization
*/
public function register( DonationEvents $events ) {
$events->onDonationCreated( function() {
$this->factory->getFirewroksService()->launchFireworks();
} );
$events->onDonationCreated( function( DonationCreatedEvent $event ) {
$this->factory->getDonationCreatedMailer()->mailConfirmation( $event->getDonationId() );
} );
// Might make sense to just have onDonationCreated here. 2 for demonstration purpouses
}
}
class SuchUseCase {
private $events;
public function __construct( DonationEvents $events ) {
$this->events = $events;
}
public function doSuch() {
$donation = $this->createDonation();
$this->events->donationCreated( new DonationCreatedEvent( $donation->getId() ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment