event programming for modular projects – Event programming – Origin : GUI



event programming for modular projects – Event programming – Origin : GUI

0 0


talks-eventProgramming

talk about envent programming for Clermontech ApiHour on July "14

On Github etienneroudeix / talks-eventProgramming

event programming for modular projects

Talking : Etienne Roudeix / @__etienne

Event programming

Origin : GUI

mouse actions - key presses

Event programming

core programming

different logic

aim : modularity

Event programming

More modular than OOP inheritance !

OOP sample

public function createUser($login, $password, $email)
{
    if (User::exists($login) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        throw new RuntimeException('Bad login or email'); //check data
    }

    $user = User::create([ //insert in database
        'login' => $login,
        'password' => $password,
        'email' => $email,
    ]);

    Mail::send($email, "Welcome", "Glad you joined us"); //send email

    return $user;
}

OOP sample

adding my own checking ?

INHERITANCE !

public function createUser($login, $password, $email)
{
    if (strlen($password) < 6) {
        throw new RuntimeException('Password is not complex enough');
    }

    return parent::createUser($login, $password, $email);
}

SO EASY

OOP sample

adding a created_at field to my user
public function createUser($login, $password, $email)
{
    if (strlen($password) < 6) {
        throw new RuntimeException('Password is not complex enough');
    }

    $user = parent::createUser($login, $password, $email);

    $user->created_at = new DateTime();
    $user->save();

    return $user;
}

NOT THAT SEXY

2 database queries

but works

OOP sample

sending the e-mail under conditions ?

OH WAIT

rewrite the whole function

update issues

EVENT PROGRAMMING WAY

public function createUser($login, $password, $email)
{
    $user = User::factory([
        'login' => $login,
        'password' => $password,
        'email' => $email,
    ]);

    Event::fire('user.create.check', $user);

    $user->save(); //insert in database

    Event::fire('user.create.send_mail', $user);
    Event::fire('user.created', $user);

    return $user;
}

EVENT PROGRAMMING WAY

have to do checking

Event::listen(
    'user.create.check',
    function(User &$user) {
        if (User::exists($user->login)
            || !filter_var($user->email, FILTER_VALIDATE_EMAIL)) {
            throw new RuntimeException('Bad login or email');
        }
    },
    0 //priority
);
Event::listen(
    'user.create.send_mail',
    function(User &$user) {
        Mail::send($user->email, "Welcome", "Glad you joined us");
    },
    0 //priority
);

EVENT PROGRAMMING WAY

adding my own checking ?

LISTEN FROM MY MODULE !

Event::listen(
    'user.create.check',
    function(User &$user) {
        if (strlen($user->password) < 6) {
            throw new RuntimeException('Password is not complex enough');
        }
    },
    10 //greater priority
);

SO EASY

EVENT PROGRAMMING WAY

adding a created_at field to my user
Event::listen(
    'user.create.check',
    function(User &$user) {
        $user->created_at = new DateTime();
    },
    -10 //lower priority
);

SO SEXY

EVENT PROGRAMMING WAY

sending the e-mail under conditions ?
Event::listen(
    'user.create.send_mail',
    function(User &$user) {
        if (!$user->worthIt()) {
            return false; // stop propagation
        }
    },
    10 //greater priority
);

SO BRILLIANT

what else ?

grouping actions

all emails in the same MailSubscriber

all admin management in AdminSubscriber

ORM

easy pre/post crud modularity

contextualisation

testing => no MailSubscriber, no LogSubscriber

prod => no DebugSubscriber

greedy ?

thank you

by Etienne Roudeix / @__etienne

http://github.roudeix.net/talks-eventProgramming