Skip to content

Commit bc65ae3

Browse files
committed
wip
1 parent 47020a2 commit bc65ae3

20 files changed

+219
-21
lines changed

app/Events/ReplyWasCreated.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\Models\Reply;
6+
use Illuminate\Queue\SerializesModels;
7+
8+
class ReplyWasCreated
9+
{
10+
use SerializesModels;
11+
12+
/**
13+
* @var \App\Models\Reply
14+
*/
15+
public $reply;
16+
17+
public function __construct(Reply $reply)
18+
{
19+
$this->reply = $reply;
20+
}
21+
}

app/Helpers/ReceivesReplies.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Helpers;
44

55
use App\Models\Reply;
6+
use App\Models\Subscription;
67
use Illuminate\Database\Eloquent\Relations\MorphMany;
78

89
trait ReceivesReplies
@@ -32,4 +33,17 @@ public function repliesRelation(): MorphMany
3233
{
3334
return $this->morphMany(Reply::class, 'replyable');
3435
}
36+
37+
/**
38+
* @return \App\Models\Subscription[]
39+
*/
40+
public function subscriptions()
41+
{
42+
return $this->subscriptionsRelation;
43+
}
44+
45+
public function subscriptionsRelation(): MorphMany
46+
{
47+
return $this->morphMany(Subscription::class, 'subscriptionable');
48+
}
3549
}

app/Jobs/CreateReply.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Jobs;
44

5+
use App\Events\ReplyWasCreated;
56
use App\User;
67
use App\Models\Reply;
78
use App\Models\ReplyAble;
@@ -49,6 +50,8 @@ public function handle(): Reply
4950
$reply->to($this->replyAble);
5051
$reply->save();
5152

53+
event(new ReplyWasCreated($reply));
54+
5255
return $reply;
5356
}
5457
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
use App\Events\ReplyWasCreated;
6+
use App\Models\Subscription;
7+
use App\Notifications\NewReply;
8+
9+
class SendNewReplyNotification
10+
{
11+
public function handle(ReplyWasCreated $event)
12+
{
13+
collect($event->reply->replyAble()->subscriptions())
14+
->each(function (Subscription $subscription) {
15+
$subscription->user()->notify(new NewReply());
16+
});
17+
}
18+
}

app/Models/ReplyAble.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,11 @@ public function latestReplies(int $amount = 5);
2222
public function deleteReplies();
2323

2424
public function repliesRelation(): MorphMany;
25+
26+
/**
27+
* @return \App\Models\Subscription[]
28+
*/
29+
public function subscriptions();
30+
31+
public function subscriptionsRelation(): MorphMany;
2532
}

app/Models/Subscription.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace App\Models;
44

5+
use App\User;
56
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
68

79
class Subscription extends Model
810
{
@@ -18,4 +20,14 @@ class Subscription extends Model
1820
'subscriptionable_id',
1921
'subscriptionable_type',
2022
];
23+
24+
public function user(): User
25+
{
26+
return $this->userRelation;
27+
}
28+
29+
public function userRelation(): BelongsTo
30+
{
31+
return $this->belongsTo(User::class);
32+
}
2133
}

app/Notifications/NewReply.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace App\Notifications;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Notifications\Notification;
7+
use Illuminate\Contracts\Queue\ShouldQueue;
8+
use Illuminate\Notifications\Messages\MailMessage;
9+
10+
class NewReply extends Notification implements ShouldQueue
11+
{
12+
use Queueable;
13+
14+
public function __construct()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Get the notification's delivery channels.
21+
*
22+
* @param mixed $notifiable
23+
* @return array
24+
*/
25+
public function via($notifiable)
26+
{
27+
return ['mail'];
28+
}
29+
30+
/**
31+
* Get the mail representation of the notification.
32+
*
33+
* @param mixed $notifiable
34+
* @return \Illuminate\Notifications\Messages\MailMessage
35+
*/
36+
public function toMail($notifiable)
37+
{
38+
return (new MailMessage)
39+
->line('The introduction to the notification.')
40+
->action('Notification Action', url('/'))
41+
->line('Thank you for using our application!');
42+
}
43+
44+
/**
45+
* Get the array representation of the notification.
46+
*
47+
* @param mixed $notifiable
48+
* @return array
49+
*/
50+
public function toArray($notifiable)
51+
{
52+
return [
53+
//
54+
];
55+
}
56+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use App\Events\ReplyWasCreated;
6+
use App\Listeners\SendNewReplyNotification;
7+
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
8+
9+
class EventServiceProvider extends ServiceProvider
10+
{
11+
/**
12+
* The event listener mappings for the application.
13+
*
14+
* @var array
15+
*/
16+
protected $listen = [
17+
ReplyWasCreated::class => [
18+
SendNewReplyNotification::class,
19+
],
20+
];
21+
22+
/**
23+
* Register any events for your application.
24+
*/
25+
public function boot()
26+
{
27+
parent::boot();
28+
29+
//
30+
}
31+
}

config/app.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
*/
174174
App\Providers\AppServiceProvider::class,
175175
App\Providers\AuthServiceProvider::class,
176+
App\Providers\EventServiceProvider::class,
176177
App\Providers\RouteServiceProvider::class,
177178
App\Markdown\MarkdownServiceProvider::class,
178179
App\Spam\SpamServiceProvider::class,

tests/Components/Jobs/BanUserTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Tests\Components\Jobs;
44

5+
use Tests\TestCase;
56
use App\Jobs\BanUser;
67
use Illuminate\Foundation\Testing\DatabaseMigrations;
78

8-
class BanUserTest extends JobTestCase
9+
class BanUserTest extends TestCase
910
{
1011
use DatabaseMigrations;
1112

0 commit comments

Comments
 (0)