{"action":"created","issue":{"url":"https://api.github.com/repos/laravel/ideas/issues/1237","repository_url":"https://api.github.com/repos/laravel/ideas","labels_url":"https://api.github.com/repos/laravel/ideas/issues/1237/labels{/name}","comments_url":"https://api.github.com/repos/laravel/ideas/issues/1237/comments","events_url":"https://api.github.com/repos/laravel/ideas/issues/1237/events","html_url":"https://github.com/laravel/ideas/issues/1237","id":335020856,"node_id":"MDU6SXNzdWUzMzUwMjA4NTY=","number":1237,"title":"Add a \"condition()\" method to determine whether a Listener should proceed","user":{"login":"fitztrev","id":271432,"node_id":"MDQ6VXNlcjI3MTQzMg==","avatar_url":"https://avatars0.githubusercontent.com/u/271432?v=4","gravatar_id":"","url":"https://api.github.com/users/fitztrev","html_url":"https://github.com/fitztrev","followers_url":"https://api.github.com/users/fitztrev/followers","following_url":"https://api.github.com/users/fitztrev/following{/other_user}","gists_url":"https://api.github.com/users/fitztrev/gists{/gist_id}","starred_url":"https://api.github.com/users/fitztrev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fitztrev/subscriptions","organizations_url":"https://api.github.com/users/fitztrev/orgs","repos_url":"https://api.github.com/users/fitztrev/repos","events_url":"https://api.github.com/users/fitztrev/events{/privacy}","received_events_url":"https://api.github.com/users/fitztrev/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-06-22T21:13:18Z","updated_at":"2018-07-28T10:04:20Z","closed_at":null,"author_association":"NONE","body":"A lot of my listeners are meant to happen only under certain Event circumstances. Because of this, in the `handle()` method of my listeners, I either wrap all the code in a conditional or do early returns if nothing needs to happen.\r\n\r\nAs an alternative, I'm proposing a `condition()` method that could be added to Listeners and then evaluated before `handle()` is called.\r\n\r\nHere is a before+after example with [the new email verification Listener](https://github.com/laravel/framework/blob/master/src/Illuminate/Auth/Listeners/SendEmailVerificationNotification.php). _(Docblocks removed for brevity)_\r\n\r\n### Current Method\r\n\r\n```php\r\n<?php\r\n\r\nnamespace Illuminate\\Auth\\Listeners;\r\n\r\nuse Illuminate\\Auth\\Events\\Registered;\r\nuse Illuminate\\Contracts\\Auth\\MustVerifyEmail;\r\n\r\nclass SendEmailVerificationNotification\r\n{\r\n    public function handle(Registered $event)\r\n    {\r\n        if ($event->user instanceof MustVerifyEmail) {\r\n            $event->user->sendEmailVerificationNotification();\r\n        }\r\n    }\r\n}\r\n```\r\n\r\n### Proposed Method\r\n\r\n```php\r\n<?php\r\n\r\nnamespace Illuminate\\Auth\\Listeners;\r\n\r\nuse Illuminate\\Auth\\Events\\Registered;\r\nuse Illuminate\\Contracts\\Auth\\MustVerifyEmail;\r\n\r\nclass SendEmailVerificationNotification\r\n{\r\n    public function condition(Registered $event)\r\n    {\r\n        return $event->user instanceof MustVerifyEmail;\r\n    }\r\n\r\n    public function handle(Registered $event)\r\n    {\r\n        $event->user->sendEmailVerificationNotification();\r\n    }\r\n}\r\n```\r\n\r\nIt would be optional. You could continue doing `if` statements inside your handle."},"comment":{"url":"https://api.github.com/repos/laravel/ideas/issues/comments/408596684","html_url":"https://github.com/laravel/ideas/issues/1237#issuecomment-408596684","issue_url":"https://api.github.com/repos/laravel/ideas/issues/1237","id":408596684,"node_id":"MDEyOklzc3VlQ29tbWVudDQwODU5NjY4NA==","user":{"login":"crnkovic","id":6536260,"node_id":"MDQ6VXNlcjY1MzYyNjA=","avatar_url":"https://avatars2.githubusercontent.com/u/6536260?v=4","gravatar_id":"","url":"https://api.github.com/users/crnkovic","html_url":"https://github.com/crnkovic","followers_url":"https://api.github.com/users/crnkovic/followers","following_url":"https://api.github.com/users/crnkovic/following{/other_user}","gists_url":"https://api.github.com/users/crnkovic/gists{/gist_id}","starred_url":"https://api.github.com/users/crnkovic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/crnkovic/subscriptions","organizations_url":"https://api.github.com/users/crnkovic/orgs","repos_url":"https://api.github.com/users/crnkovic/repos","events_url":"https://api.github.com/users/crnkovic/events{/privacy}","received_events_url":"https://api.github.com/users/crnkovic/received_events","type":"User","site_admin":false},"created_at":"2018-07-28T10:04:20Z","updated_at":"2018-07-28T10:04:20Z","author_association":"NONE","body":"Looks pretty useful! I dig the name `when($event)` more than `condition($event)`, and could default to `true` (if method not specified)"}}
{"actor":{"display_login":"crnkovic"}}