Skip to main content

Bottelet/DaybydayCRM

2311 stars
4 code files
View Bottelet/DaybydayCRM on GitHub

app/Listeners/ProjectActionNotify.php

Open in GitHub
// This is the Listener class - it takes the Event object and processes the details to notify the assignee
// whenever some Project Action happens
 
class ProjectActionNotify
{
public function handle(ProjectAction $event)
{
$Project = $event->getProject();
$action = $event->getAction();
$Project->assignee->notify(new ProjectActionNotification(
$Project,
$action
));
}
}

app/Providers/EventServiceProvider.php

Open in GitHub
// This is the default EventServiceProvider that comes with Laravel, it assigned the Events to their Listeners
// As in this example, it is possible to attach multiple Listeners to one Event
 
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
// ...
 
'App\Events\ProjectAction' => [
'App\Listeners\ProjectActionNotify',
'App\Listeners\ProjectActionLog'
]
];
}

app/Events/ProjectAction.php

Open in GitHub
// The Event class: all it does is taking care of the parameters
// Also it has a few so-called "getters" method (used in Listener) to get the private properties
 
class ProjectAction
{
private $project;
private $action;
 
public function getProject()
{
return $this->project;
}
 
public function getAction()
{
return $this->action;
}
 
public function __construct(Project $project, $action)
{
$this->project = $project;
$this->action = $action;
}
}

app/Http/Controllers/ProjectsController.php

Open in GitHub
// We call that Event from the Controller, passing the parameters of Project and Action.
 
class ProjectsController extends Controller
{
const CREATED = 'created';
 
public function store(StoreProjectRequest $request)
{
$project = Project::create(
[
'title' => $request->title,
'description' => clean($request->description),
'user_assigned_id' => $request->user_assigned_id,
'deadline' => Carbon::parse($request->deadline),
'status_id' => $request->status_id,
'user_created_id' => auth()->id(),
'external_id' => Uuid::uuid4()->toString(),
'client_id' => $client ? $client->id : null,
]
);
 
event(new \App\Events\ProjectAction($project, self::CREATED));
}
}