Skip to content

Commit 0eac1dd

Browse files
committed
Episode 35: Notify All Voters Test
1 parent ed2adc6 commit 0eac1dd

16 files changed

+1305
-20
lines changed

app/Http/Livewire/SetStatus.php

+3-15
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace App\Http\Livewire;
44

5+
use App\Jobs\NotifyAllVoters;
56
use App\Models\Idea;
67
use Livewire\Component;
78
use Illuminate\Http\Response;
8-
use Illuminate\Support\Facades\Mail;
9-
use App\Mail\IdeaStatusUpdatedMailable;
9+
1010

1111
class SetStatus extends Component
1212
{
@@ -30,23 +30,11 @@ public function setStatus()
3030
$this->idea->save();
3131

3232
if ($this->notifyAllVoters) {
33-
$this->notifyAllVoters();
33+
NotifyAllVoters::dispatch($this->idea);
3434
}
3535

3636
$this->emit('statusWasUpdated');
3737
}
38-
39-
public function notifyAllVoters()
40-
{
41-
$this->idea->votes()
42-
->select('name', 'email')
43-
->chunk(100, function ($voters) {
44-
foreach ($voters as $user) {
45-
Mail::to($user)
46-
->queue(new IdeaStatusUpdatedMailable($this->idea));
47-
}
48-
});
49-
}
5038

5139
public function render()
5240
{

app/Jobs/NotifyAllVoters.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\Models\Idea;
6+
use Illuminate\Bus\Queueable;
7+
use Illuminate\Support\Facades\Mail;
8+
use Illuminate\Queue\SerializesModels;
9+
use App\Mail\IdeaStatusUpdatedMailable;
10+
use Illuminate\Queue\InteractsWithQueue;
11+
use Illuminate\Contracts\Queue\ShouldQueue;
12+
use Illuminate\Foundation\Bus\Dispatchable;
13+
use Illuminate\Contracts\Queue\ShouldBeUnique;
14+
15+
class NotifyAllVoters implements ShouldQueue
16+
{
17+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
18+
19+
public $idea;
20+
21+
/**
22+
* Create a new job instance.
23+
*
24+
* @return void
25+
*/
26+
public function __construct(Idea $idea)
27+
{
28+
$this->idea = $idea;
29+
}
30+
31+
/**
32+
* Execute the job.
33+
*
34+
* @return void
35+
*/
36+
public function handle()
37+
{
38+
$this->idea->votes()
39+
->select('name', 'email')
40+
->chunk(100, function ($voters) {
41+
foreach ($voters as $user) {
42+
Mail::to($user)
43+
->queue(new IdeaStatusUpdatedMailable($this->idea));
44+
}
45+
});
46+
}
47+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use Illuminate\Support\Facades\Gate;
6+
use Laravel\Horizon\Horizon;
7+
use Laravel\Horizon\HorizonApplicationServiceProvider;
8+
9+
class HorizonServiceProvider extends HorizonApplicationServiceProvider
10+
{
11+
/**
12+
* Bootstrap any application services.
13+
*
14+
* @return void
15+
*/
16+
public function boot()
17+
{
18+
parent::boot();
19+
20+
// Horizon::routeSmsNotificationsTo('15556667777');
21+
// Horizon::routeMailNotificationsTo('[email protected]');
22+
// Horizon::routeSlackNotificationsTo('slack-webhook-url', '#channel');
23+
24+
Horizon::night();
25+
}
26+
27+
/**
28+
* Register the Horizon gate.
29+
*
30+
* This gate determines who can access Horizon in non-local environments.
31+
*
32+
* @return void
33+
*/
34+
protected function gate()
35+
{
36+
Gate::define('viewHorizon', function ($user) {
37+
return in_array($user->email, [
38+
39+
]);
40+
});
41+
}
42+
}

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"fruitcake/laravel-cors": "^2.0",
1212
"guzzlehttp/guzzle": "^7.0.1",
1313
"laravel/framework": "^8.12",
14+
"laravel/horizon": "^5.7",
1415
"laravel/tinker": "^2.5",
1516
"livewire/livewire": "^2.4"
1617
},

composer.lock

+78-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/app.php

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
App\Providers\AuthServiceProvider::class,
174174
// App\Providers\BroadcastServiceProvider::class,
175175
App\Providers\EventServiceProvider::class,
176+
App\Providers\HorizonServiceProvider::class,
176177
App\Providers\RouteServiceProvider::class,
177178

178179
],

0 commit comments

Comments
 (0)