Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.

Commit c5ea040

Browse files
committed
merge addAll and deleteAll to singular methods of room class
1 parent e825589 commit c5ea040

File tree

5 files changed

+20
-50
lines changed

5 files changed

+20
-50
lines changed

src/Websocket/Rooms/RedisRoom.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,22 @@ public function getRedis()
6363
return $this->redis;
6464
}
6565

66-
public function add(int $fd, string $room)
66+
public function add(int $fd, $roomNames)
6767
{
68-
$this->addAll($fd, [$room]);
69-
}
68+
$roomNames = is_array($roomNames) ? $roomNames : [$roomNames];
7069

71-
public function addAll(int $fd, array $roomNames)
72-
{
7370
$this->addValue($fd, $roomNames, 'fds');
7471

7572
foreach ($roomNames as $room) {
7673
$this->addValue($room, [$fd], 'rooms');
7774
}
7875
}
7976

80-
public function delete(int $fd, string $room)
81-
{
82-
$this->deleteAll($fd, [$room]);
83-
}
84-
85-
public function deleteAll(int $fd, array $roomNames = [])
77+
public function delete(int $fd, $roomNames = [])
8678
{
79+
$roomNames = is_array($roomNames) ? $roomNames : [$roomNames];
8780
$roomNames = count($roomNames) ? $roomNames : $this->getRooms($fd);
81+
8882
$this->removeValue($fd, $roomNames, 'fds');
8983

9084
foreach ($roomNames as $room) {

src/Websocket/Rooms/RoomContract.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,17 @@ public function prepare();
1313
* Add a socket to a room.
1414
*
1515
* @int fd
16-
* @string room
16+
* @string|array rooms
1717
*/
18-
public function add(int $fd, string $room);
19-
20-
/**
21-
* Add a socket to multiple rooms.
22-
*
23-
* @int fd
24-
* @array room
25-
*/
26-
public function addAll(int $fd, array $rooms);
18+
public function add(int $fd, $rooms);
2719

2820
/**
2921
* Delete a socket from a room.
3022
*
3123
* @int fd
32-
* @string room
33-
*/
34-
public function delete(int $fd, string $room);
35-
36-
/**
37-
* Delete a socket from all rooms.
38-
*
39-
* @int fd
40-
* @string room
24+
* @string|array rooms
4125
*/
42-
public function deleteAll(int $fd);
26+
public function delete(int $fd, $rooms);
4327

4428
/**
4529
* Get all sockets by a room key.

src/Websocket/Rooms/TableRoom.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,10 @@ public function prepare()
2424
$this->initFdsTable();
2525
}
2626

27-
public function add(int $fd, string $room)
28-
{
29-
$this->addAll($fd, [$room]);
30-
}
31-
32-
public function addAll(int $fd, array $roomNames)
27+
public function add(int $fd, $roomNames)
3328
{
3429
$rooms = $this->getRooms($fd);
30+
$roomNames = is_array($roomNames) ? $roomNames : [$roomNames];
3531

3632
foreach ($roomNames as $room) {
3733
$fds = $this->getClients($room);
@@ -49,14 +45,10 @@ public function addAll(int $fd, array $roomNames)
4945
$this->setRooms($fd, $rooms);
5046
}
5147

52-
public function delete(int $fd, string $room)
53-
{
54-
$this->deleteAll($fd, [$room]);
55-
}
56-
57-
public function deleteAll(int $fd, array $roomNames = [])
48+
public function delete(int $fd, $roomNames = [])
5849
{
5950
$allRooms = $this->getRooms($fd);
51+
$roomNames = is_array($roomNames) ? $roomNames : [$roomNames];
6052
$rooms = count($roomNames) ? $roomNames : $allRooms;
6153

6254
$removeRooms = [];

tests/Websocket/RedisRoomTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testAddAll()
5151
->times(3);
5252
$redisRoom = $this->getRedisRoom($redis);
5353

54-
$redisRoom->addAll(1, ['foo', 'bar']);
54+
$redisRoom->add(1, ['foo', 'bar']);
5555
}
5656

5757
public function testAdd()
@@ -71,7 +71,7 @@ public function testDeleteAll()
7171
->times(3);
7272
$redisRoom = $this->getRedisRoom($redis);
7373

74-
$redisRoom->deleteAll(1, ['foo', 'bar']);
74+
$redisRoom->delete(1, ['foo', 'bar']);
7575
}
7676

7777
public function testDelete()

tests/Websocket/TableRoomTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function testSetValue()
5454

5555
public function testAddAll()
5656
{
57-
$this->tableRoom->addAll($key = 1, $values = ['foo', 'bar']);
57+
$this->tableRoom->add($key = 1, $values = ['foo', 'bar']);
5858

5959
$this->assertSame($values, $this->tableRoom->getValue($key, $table = 'fds'));
6060
$this->assertSame([$key], $this->tableRoom->getValue('foo', 'rooms'));
@@ -71,8 +71,8 @@ public function testAdd()
7171

7272
public function testDeleteAll()
7373
{
74-
$this->tableRoom->addAll($key = 1, $values = ['foo', 'bar']);
75-
$this->tableRoom->deleteAll($key);
74+
$this->tableRoom->add($key = 1, $values = ['foo', 'bar']);
75+
$this->tableRoom->delete($key);
7676

7777
$this->assertSame([], $this->tableRoom->getValue($key, $table = 'fds'));
7878
$this->assertSame([], $this->tableRoom->getValue('foo', 'rooms'));
@@ -81,7 +81,7 @@ public function testDeleteAll()
8181

8282
public function testDelete()
8383
{
84-
$this->tableRoom->addAll($key = 1, $values = ['foo', 'bar']);
84+
$this->tableRoom->add($key = 1, $values = ['foo', 'bar']);
8585
$this->tableRoom->delete($key, 'foo');
8686

8787
$this->assertSame(['bar'], $this->tableRoom->getValue($key, $table = 'fds'));
@@ -91,7 +91,7 @@ public function testDelete()
9191

9292
public function testGetRooms()
9393
{
94-
$this->tableRoom->addAll($key = 1, $values = ['foo', 'bar']);
94+
$this->tableRoom->add($key = 1, $values = ['foo', 'bar']);
9595

9696
$this->assertSame(
9797
$this->tableRoom->getValue($key, $table = 'fds'),

0 commit comments

Comments
 (0)