-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8951b8c
commit 61d3f23
Showing
10 changed files
with
187 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,5 @@ parameters: | |
|
||
# Level 9 is the highest level | ||
level: 6 | ||
|
||
checkGenericClassInNonGenericObjectType: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace SlashEquip\LaravelSegment\Contracts; | ||
|
||
interface CanNotifyViaSegment | ||
{ | ||
public function toSegment(CanBeIdentifiedForSegment $notifiable): CanBeSentToSegment; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/Exceptions/NotifiableCannotBeIdentifiedForSegmentException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace SlashEquip\LaravelSegment\Exceptions; | ||
|
||
use RuntimeException; | ||
|
||
class NotifiableCannotBeIdentifiedForSegmentException extends RuntimeException | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace SlashEquip\LaravelSegment\Notifications; | ||
|
||
use SlashEquip\LaravelSegment\Contracts\CanBeIdentifiedForSegment; | ||
use SlashEquip\LaravelSegment\Contracts\CanNotifyViaSegment; | ||
use SlashEquip\LaravelSegment\Exceptions\NotifiableCannotBeIdentifiedForSegmentException; | ||
use SlashEquip\LaravelSegment\Facades\Segment; | ||
|
||
class SegmentChannel | ||
{ | ||
public function send(object $notifiable, CanNotifyViaSegment $notification): void | ||
{ | ||
if (! $notifiable instanceof CanBeIdentifiedForSegment) { | ||
throw new NotifiableCannotBeIdentifiedForSegmentException(); | ||
} | ||
|
||
Segment::push($notification->toSegment($notifiable)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace SlashEquip\LaravelSegment\Tests\Stubs; | ||
|
||
use Illuminate\Notifications\Notification; | ||
use Illuminate\Support\Str; | ||
use SlashEquip\LaravelSegment\Contracts\CanBeIdentifiedForSegment; | ||
use SlashEquip\LaravelSegment\Contracts\CanBeSentToSegment; | ||
use SlashEquip\LaravelSegment\Contracts\CanNotifyViaSegment; | ||
use SlashEquip\LaravelSegment\Notifications\SegmentChannel; | ||
use SlashEquip\LaravelSegment\SimpleSegmentEvent; | ||
|
||
class SegmentTestNotification extends Notification implements CanNotifyViaSegment | ||
{ | ||
public function __construct( | ||
private int $number | ||
) { | ||
} | ||
|
||
public function via(object $notifiable): array | ||
{ | ||
return [SegmentChannel::class]; | ||
} | ||
|
||
public function toSegment(CanBeIdentifiedForSegment $notifiable): CanBeSentToSegment | ||
{ | ||
return new SimpleSegmentEvent( | ||
$notifiable, | ||
Str::of(class_basename(static::class)) | ||
->snake() | ||
->replace('_', ' ') | ||
->title(), | ||
['some' => 'thing'], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
use SlashEquip\LaravelSegment\Contracts\SegmentServiceContract; | ||
use SlashEquip\LaravelSegment\SimpleSegmentEvent; | ||
use SlashEquip\LaravelSegment\Tests\Stubs\SegmentTestNotification; | ||
use SlashEquip\LaravelSegment\Tests\Stubs\SegmentTestUser; | ||
|
||
it('can send a notification to a notifiable entity', function () { | ||
// Given we have a notifiable entity | ||
$notifiable = new SegmentTestUser('123456'); | ||
|
||
// And we are spying on the service | ||
$service = test()->spy(SegmentServiceContract::class); | ||
|
||
// When we notify the entity | ||
$notifiable->notify(new SegmentTestNotification(987654)); | ||
|
||
// Then the service was called appropriately | ||
$service->shouldHaveReceived('push') | ||
->with(Mockery::on(function ($arg) { | ||
if (! $arg instanceof SimpleSegmentEvent) { | ||
return false; | ||
} | ||
|
||
$payload = $arg->toSegment(); | ||
|
||
return $payload->user->getSegmentIdentifier() === '123456' | ||
&& $payload->event === 'Segment Test Notification' | ||
&& count($payload->data) === 1 | ||
&& $payload->data['some'] === 'thing'; | ||
})) | ||
->once(); | ||
}); |