PHP code example of ufo-engineering / ko-events

1. Go to this page and download the library: Download ufo-engineering/ko-events library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ufo-engineering / ko-events example snippets


return [
    'events_handlers' => [
        'Event_Test'       => ['Handler_EventTest'], //Event_Test is real class in dir application/classes/Event/Test.php
        'someTestMethod'   => ['Handler_EventTest'] // alias, for example method name
        //...
        ]
];


/**
 * Class Event_Test
 *
 * Fired when we do something
 */
class Event_Test
{
    /**
     * @var Model_Test
     */
    public $test;

    /**
     * @param Model_Test $test
     */
    function __construct(Model_Test $test)
    {
        $this->test = $test;
    }
}

/**
 * Class Handler_EventTest
 *
 * For example: sends an email notification to user about successful registration
 */
class Handler_EventTest implements Ufo\Handler\EventHandler
{
    /**
     * Handle an event
     *
     * @param Handler_EventTest $event
     *
     * @return bool|null
     */
    public function handle($event)
    {
        // TODO send email
    }
}

use Ufo\Event;

Event::fire(new Event_Test($user));
OR
Event::fire('someTestMethod', $user);

/**
 * Class Event_Handler_User_SendWelcomeEmail
 *
 * Sends an email notification to user about successful registration
 */
class Event_Handler_User_SendWelcomeEmail extends Ufo\Handler\Notification\EventHandlerNotification implements Ufo\Handler\EventHandler
{
    /**
     * Handle an event
     *
     * @param Event_User_Registered $event
     *
     * @return bool|null
     */
    public function handle($event)
    {
        $this->send($event, 'Welcome aboard user ' . $event->user->username . '!');
    }

    /**
     * Format email notification subject
     *
     * @param Event_User_Registered $event
     *
     * @return string
     */
    protected function formatSubject($event)
    {
        return 'Welcome ' . $user->username;
    }

    /**
     * Get notification receivers list
     *
     * @param Event_User_Registered $event
     *
     * @return array
     */
    protected function getReceivers($event)
    {
        return array(
            $event->user->email => $event->user->username
        );
    }
}