I am looking for an extremely simple example of Events and Handlers so I can understand their purpose.
Here’s an imaginary, simple scenario:
-There is a book store yii app with Sellers, Buyers, and Orders. Sellers only have a name and id, Buyers only have a name and id.
-Orders have an id and description of the order. Orders have 1 buyer and 1 seller involved. Orders can become paid.
-In fact, it’s the event of a payment happening, that I’m trying to use as a simple example of Events, handlers, etc.
I started with the skeleton app created by the command line, and then added these files that I created:
Here is some code:
customer.php, seller.php, order.php, and orderevent.php (which i placed into the extensions folder which i’m including via config.php), contain the following:
// WARNING: I WOULD NOT USE THIS CODE;
// IT SEEMS TO WORK, BUT IT'S BEEN
// GENERATED BY A NEWBIE TO ASK FOR CRITICISM
class Customer extends CComponent
{
public $id;
public $name;
public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
}
public function notify($event)
{
echo 'Send an email to customer #' . $this->id . ' confirming that their purchase (Order #' . $event->order->id . ') went through!<br/>';
}
}
class Seller extends CComponent
{
public $id;
public $name;
public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
}
public function notify($event)
{
echo 'Send an email to seller #' . $this->id . ' informing them that they sold something (Order #' . $event->order->id . ')!<br/>';
}
}
class Order extends CComponent
{
public $id;
public $description;
public $seller;
public $customer;
public $paid;
public function __construct($id, $description)
{
$this->id = $id;
$this->description = $description;
}
public function processPayment()
{
if (!$this->paid)
{
$this->paid = true;
$orderEvent = new OrderEvent;
$orderEvent->order = $this;
$this->onPaymentProcessed($orderEvent);
}
}
public function onPaymentProcessed($event)
{
$this->raiseEvent('onPaymentProcessed', $event);
}
}
class OrderEvent extends CEvent
{
public $order;
}
I added this to SiteController:
public function actionSimulateSale()
{
echo 'Starting simulation.<br/>';
$order = new Order(50, 'Poster of the Rocky Mountains');
$seller = new Seller(33, 'American Poster Emporium');
$customer = new Customer(37, 'Eva Jones');
$order->seller = $seller;
$order->customer = $customer;
echo 'Order #' . $order->id . ' has been created; the Seller involved is Seller #' . $order->seller->id . ', and the Customer involved is Customer #' . $order->customer->id . '.<br/>';
$order->onPaymentProcessed = array($seller, 'notify');
$order->onPaymentProcessed = array($customer, 'notify');
$order->processPayment();
echo 'Done with simulation.<br/>';
}
Here is what happens when you visit
localhost/site/simulateSale :
Starting simulation.
Order #50 has been created; the Seller involved is Seller #33, and the Customer involved is Customer #37.
Send an email to seller #33 informing them that they sold something (Order #50)!
Send an email to customer #37 confirming that their purchase (Order #50) went through!
Done with simulation.
So, here are questions:
1- Is anything i’m doing weird/ wrong, or correct/ normal?
2- I’m not happy that i put
$order->onPaymentProcessed = array($seller, 'notify');
$order->onPaymentProcessed = array($customer, 'notify');
within the controller because it seems like generalized application logic that’s not really limited to the controllersphere.
However, I can’t figure out how to put stuff elsewhere to accomplish the same thing. How can that be done?
3- What’s the point of doing all this “fancy” stuff instead of simply not using Events, handlers, etc.,
and instead simply doing something like this at the bottom of the Order class’s processPayment function?
$this->customer->notify($this);
etc etc
(which would involve also changing Customer class’s notify() function to accept $order instead of $event, of course… in other words ditching the Event/ handler paradigm and just doing things directly.
It would be great if anyone could stick with the theme here of using book-sales-related concepts (or actually, i guess Eva Jones bought a poster, darn her!) in your replies rather than replying purely theoretically… Thanks I hope this gets a response!