Last active
March 14, 2019 17:12
-
-
Save jpotts18/62beae3ca85032bd25bc5944e1f44ca8 to your computer and use it in GitHub Desktop.
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
<?php | |
// https://codeigniter.com/userguide3/general/creating_libraries.html | |
class EventPublisher { | |
const SOURCE = 'admin' | |
public function __construct($params, $resource_name) | |
{ | |
$this->CI =& get_instance(); | |
$this->resource = $resource_name; | |
// hold a single rabbitMQ connection. | |
$this->connection; | |
} | |
public function publish($event_name, $payload) | |
{ | |
$exchange_name = $this->build_exchange_name($this->resource); | |
$event = $this->build_event($this->resource, $event_name, $payload); | |
$routing_key = $this->build_routing_key($this->resource, $event_name); | |
$channel = $this->connection->channel; | |
$channel->exchange_declare($exchange_name, 'topic', false, false, false); | |
$msg = new AMQPMessage($event); | |
$channel->basic_publish($msg, $exchange_name, $routing_key); | |
$channel->close(); | |
} | |
private function build_routing_key($resource, $event_name) | |
{ | |
// source.resource.event_name | |
// admin.group.created | |
// admin.group.deleted | |
return SOURCE . '.' . $resource . '.' . $event_name | |
} | |
private function build_exchange_name($resource) | |
{ | |
// lower case | |
// source.resource | |
// admin.group | |
return SOURCE . '.' . $resource | |
} | |
private function build_event($resource, $event_name, $payload) | |
{ | |
/* | |
return { | |
eventType: capitalize($resource) . '.' . captialize($event_name), | |
// eventType: 'GroupCreated', | |
payload: $payload | |
eventTimestamp: ISO8601 UTC | |
} | |
*/ | |
} | |
} | |
?> |
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
<?php | |
class Group_model extends CI_Model { | |
public function __construct($params) | |
{ | |
// somehow we need to configure the resource name | |
$this->load('event_publisher'); | |
$this->event->resource('Group') | |
} | |
public function create() | |
{ | |
$this->name = $_POST['name']; | |
$this->db->insert('groups', $this); | |
$this->event->publish('created', $this); | |
} | |
public function update() | |
{ | |
$this->name = $_POST['name']; | |
$this->db->update('groups', $this, array('id' => $_POST['id'])); | |
$this->event->publish('created', $this); | |
} | |
public function delete() | |
{ | |
$this->name = $_POST['name']; | |
// need to send the full object that has been deleted | |
$result = $this->db->get('groups') // where id = $POST_ID | |
$this->db->delete('groups', arrapy('id' => $POST['id'])); | |
$this->event->publish('deleted', $result); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment