mardi 12 janvier 2016

How to trigger a listener automatically once and event is completed

I have 2 separate packages running on Laravel 5.2. The main package is called "surveys" while the other once is called "kicker". Kicker's package depends on "surveys" to run.

In the "surveys" I fire an event called "SurveyWasCompleted" when a survey is completed. I need to listen for that events in my "kicker" package. Once the event occurs, I want to run couple maintenance queries with in my "Kickers" package.

Also, I want to pass an object from the event to the listener.

This is how my event class look like

<?php namespace Mikea\Surveys\Events\Interviews;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class InterviewWasCompleted extends Event
{

    protected $interview;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct($interview)
    {
        $this->interview = $interview;
    }


    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return [];
    }
}

This is how my listener class look like

<?php

namespace Mikea\Kicker\Listeners;

use App\Listeners\Listener;
use Rdicorp\Surveys\Tasks\Interviews\SurveyWasCompleted;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class IncreaseStoreQuota extends Listener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        dd('IncreaseStoreQuota Listener');
    }

    /**
     * Handle the event.
     *
     * @param  SurveyWasCompleted  $event
     * @return void
     */
    public function handle(SurveyWasCompleted $event)
    {
        dd('IncreaseStoreQuota Listener', $event);
        //Define the logic to be executed when an survey was completed
    }
}

Then I created an EventServiceProvider for my package like this

<?php

namespace Mikea\Kicker\Providers;

use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        'Mikea\Surveys\Events\Interviews\SurveyWasCompleted' => [
            'Mikea\Kicker\Listeners\IncreaseStoreQuota',
        ],
    ];

    /**
     * Register any other events for your application.
     *
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
     * @return void
     */
    public function boot(DispatcherContract $events)
    {
        parent::boot($events);

    }
}

Finally, I load my events service provides by adding the following code to the app.php file

Mikea\Kicker\Providers\EventServiceProvider::class,

How can I fire my listener correctly? also how can I pass the variable $this->interview from the event to the listener?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire