Skip to content

Commit

Permalink
fix: dont throw exceptions to users (#44)
Browse files Browse the repository at this point in the history
* fix: dont throw exceptions to users

* fix phpcs

* Apply suggestions from code review

* fix phpstan errors

* update tests

---------

Co-authored-by: Kieran <[email protected]>
  • Loading branch information
jshah4517 and bytestream committed Mar 11, 2024
1 parent a22df6b commit 4c91a72
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 15 deletions.
16 changes: 12 additions & 4 deletions src/Http/Controller/ChannelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,24 @@ public function subscribe(SubscribeRequest $request): JsonResponse

public function unsubscribe(UnsubscribeRequest $request): JsonResponse
{
/** @var Channel $channel */
/** @var Channel|null $channel */
$channel = Channel::query()
->where('name', $request->channel_name)
->firstOrFail();
->first();

if ($channel === null) {
return new JsonResponse([false]);
}

/** @var Member $member */
/** @var Member|null $member */
$member = Member::query()
->where('channel_id', $channel->id)
->where('socket_id', $this->socket->id())
->firstOrFail();
->first();

if ($member === null) {
return new JsonResponse([false]);
}

$this->socket->removeMemberFromChannel($member, $channel);

Expand Down
7 changes: 6 additions & 1 deletion src/Http/Controller/PublishController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ class PublishController
*/
public function publish(PublishRequest $request): JsonResponse
{
/** @var Channel|null $channel */
$channel = Channel::query()
->where('name', $request->channel_name)
->firstOrFail();
->first();

if ($channel === null) {
return new JsonResponse([false]);
}

(new Message([
'channel_id' => $channel->id,
Expand Down
3 changes: 1 addition & 2 deletions src/Http/Controller/SubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace SupportPal\Pollcast\Http\Controller;

use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -33,7 +32,7 @@ public function messages(ReceiveRequest $request): JsonResponse
$memberQuery = Member::query()->where('socket_id', $this->socket->id());
$members = $memberQuery->get();
if ($members->isEmpty()) {
throw (new ModelNotFoundException)->setModel(Member::class);
return new JsonResponse(['status' => 'error']);
}

// Update the last active time of the member.
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Channel extends Model
/** @var string[] */
protected $guarded = [];

/** @var string[] */
/** @var array<int, string> */
protected $fillable = ['name'];

/** @var array<string, string> */
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Member.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Member extends Model
/** @var string[] */
protected $guarded = [];

/** @var string[] */
/** @var array<int, string> */
protected $fillable = ['channel_id', 'socket_id', 'data'];

/** @var array<string, string> */
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Message extends Model
/** @var string[] */
protected $guarded = [];

/** @var string[] */
/** @var array<int, string> */
protected $fillable = ['channel_id', 'member_id', 'event', 'payload'];

/** @var array<string, string> */
Expand Down
9 changes: 6 additions & 3 deletions tests/Functional/Controller/ChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ public function testUnsubscribeGuardedChannel(): void
public function testUnsubscribeChannelNotFound(): void
{
$this->postAjax(route('supportpal.pollcast.unsubscribe'), ['channel_name' => 'fake-channel'])
->assertStatus(404);
->assertStatus(200)
->assertJson([false]);
}

public function testUnsubscribeMemberNotFound(): void
Expand All @@ -139,7 +140,8 @@ public function testUnsubscribeMemberNotFound(): void
$this->setupChannel($channelName);

$this->postAjax(route('supportpal.pollcast.unsubscribe'), ['channel_name' => $channelName])
->assertStatus(404);
->assertStatus(200)
->assertJson([false]);
}

public function testUnsubscribeMemberDifferentSocketId(): void
Expand All @@ -150,7 +152,8 @@ public function testUnsubscribeMemberDifferentSocketId(): void
Member::factory()->create(['channel_id' => $channel->id]);

$this->postAjax(route('supportpal.pollcast.unsubscribe'), ['channel_name' => $channelName])
->assertStatus(404);
->assertStatus(200)
->assertJson([false]);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/Functional/Controller/PublishTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public function testPublishChannelNotFound(): void
'event' => 'test-event',
'data' => ['user_id' => 1],
])
->assertStatus(404);
->assertStatus(200)
->assertJson([false]);
}

public function testPublishValidation(): void
Expand Down
3 changes: 2 additions & 1 deletion tests/Functional/Controller/SubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ public function testMessagesMemberNotFound(): void
'channels' => ['fake-channel'],
'time' => Carbon::now()->toDateTimeString('microsecond'),
])
->assertStatus(404);
->assertStatus(200)
->assertJson(['status' => 'error']);
}

public function testMessagesValidation(): void
Expand Down

0 comments on commit 4c91a72

Please sign in to comment.