Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where to dispatch jobs/send events #11

Closed
josh-taylor opened this issue Jul 26, 2016 · 6 comments
Closed

Where to dispatch jobs/send events #11

josh-taylor opened this issue Jul 26, 2016 · 6 comments

Comments

@josh-taylor
Copy link
Contributor

My normal approach for dispatching jobs/sending events would be in the controller action, for example here where we would send an email that a model has been updated:

public function update(Model $model, Request $request) {
    $model->update($request->all());

    dispatch(new SendModelUpdatedEmail($model));

    // ...
}

What would be the recommended approach when extending from the EloquentController to achieve something similar?

My initial thoughts would be to:

  1. Override the update() method on the controller
    • Would have to duplicate the code used in the parent class.
  2. Using Eloquents updated event
    • For me, this logic shouldn't be handled in the Model.
@lindyhopchris
Copy link
Member

Hi Josh.

Your totally right, this would be best on the controller rather than a model listener. In the current version you'd need to overload the commit method:
https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Http/Controllers/EloquentController.php#L239

For example:

protected function commit(Model $model)
{
    $isUpdate = $model->exists;
    $result = parent::commit($model):

    if ($result && $isUpdate) {
        dispatch(new SomeEvent($model));
    }

    return $result;
}

That'll work with the current version (0.4.0).

A slightly better solution might be for me to put in protected methods - didUpdate, didCreate for the those two instances and didCommit that gets called for both create/update. What do you think about that as an approach? It would mean instead of the above you could do:

protected function didUpdate(Model $model)
{
    dispatch(new SomeEvent($model));
}

@josh-taylor
Copy link
Contributor Author

Yeah, that seems like a good solution to me.

I guess there could also be a beforeUpdate/beforeCreate/beforeCommit too?

Maybe the naming convention could be similar to Eloquent?

protected function updating(Model $model) { // ... }

protected function updated(Model $model) { // ... }

What do you think? I don't mind helping out with a PR for this.

@lindyhopchris
Copy link
Member

I like the idea of using the Eloquent names - I'd normally use willUpdate/didUpdate, but in terms of it being more intuitive for people who don't know my quirks the Eloquent names make sense!

I'm happy for you to submit a PR if you have time. You need to submit against the develop branch. The change should be non-breaking so can be included in the 0.4.1 release I'm preparing (you'll see a number of non-breaking changes on the develop branch at the moment).

@lindyhopchris
Copy link
Member

Oh, if you are doing a PR you don't need to worry about tests. I haven't got this package unit tested at the moment as it's fully integration tested in two production apps. So I'd check that your PR doesn't break those apps before merging.

(I am planning on adding unit tests in the future but at the moment I've got close to 100% test coverage with the integration tests, so to save development time I'm relying on those.)

@josh-taylor
Copy link
Contributor Author

Ok, no problem. I should be able to take a look at it this evening.

josh-taylor pushed a commit to josh-taylor/laravel-json-api that referenced this issue Jul 26, 2016
Adds callbacks for creating/created/updating/updated models on a
controller.

Callbacks can be defined in controllers that inherit the
`EloquentController`.

``php
class UsersController extends EloquentController
{
    protected function creating(Model $model)
    {
        // Before the model is created.
    }

    protected function created(Model $model)
    {
        // After the model has been created.
    }

    protected function updating(Model $model)
    {
        // Before the model is updated.
    }

    protected function updated()
    {
        // After the model has been created.
    }
}
``

---

See cloudcreativity#11 for a bit more discussion.`
josh-taylor pushed a commit to josh-taylor/laravel-json-api that referenced this issue Jul 27, 2016
Adds callbacks for creating/created/updating/updated models on a
controller.

Callbacks can be defined in controllers that inherit the
`EloquentController`.

```php
class UsersController extends EloquentController
{
    protected function creating(Model $model)
    {
        // Before the model is created.
    }

    protected function created(Model $model)
    {
        // After the model has been created.
    }

    protected function updating(Model $model)
    {
        // Before the model is updated.
    }

    protected function updated()
    {
        // After the model has been created.
    }
}
```

---

See cloudcreativity#11 for a bit more discussion.`
@lindyhopchris
Copy link
Member

@josh-taylor thanks for your input! This has been included in the v0.4.1 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants