Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 86fbf76

Browse files
committed
Refactored some functions
1 parent f04cce7 commit 86fbf76

File tree

1 file changed

+49
-13
lines changed

1 file changed

+49
-13
lines changed

src/ChannelManagers/RedisChannelManager.php

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,8 @@ public function subscribeToChannel(ConnectionInterface $connection, string $chan
130130
}
131131
});
132132

133-
$this->getPublishClient()->sadd(
134-
$this->getRedisKey($connection->app->id, null, ['channels']),
135-
$channelName
133+
$this->addChannelToSet(
134+
$connection->app->id, $channelName
136135
);
137136

138137
$this->incrementSubscriptionsCount(
@@ -157,25 +156,19 @@ public function unsubscribeFromChannel(ConnectionInterface $connection, string $
157156
if ($count === 0) {
158157
$this->unsubscribeFromTopic($connection->app->id, $channelName);
159158

160-
$this->getPublishClient()->srem(
161-
$this->getRedisKey($connection->app->id, null, ['channels']),
162-
$channelName
163-
);
159+
$this->removeChannelFromSet($connection->app->id, $channelName);
164160

165161
return;
166162
}
167163

168-
$increment = $this->incrementSubscriptionsCount(
169-
$connection->app->id, $channelName, -1
164+
$this->decrementSubscriptionsCount(
165+
$connection->app->id, $channelName,
170166
)
171167
->then(function ($count) use ($connection, $channelName) {
172168
if ($count < 1) {
173169
$this->unsubscribeFromTopic($connection->app->id, $channelName);
174170

175-
$this->getPublishClient()->srem(
176-
$this->getRedisKey($connection->app->id, null, ['channels']),
177-
$channelName
178-
);
171+
$this->removeChannelFromSet($connection->app->id, $channelName);
179172
}
180173
});
181174
});
@@ -456,6 +449,49 @@ public function incrementSubscriptionsCount($appId, string $channel = null, int
456449
);
457450
}
458451

452+
/**
453+
* Decrement the subscribed count number.
454+
*
455+
* @param string|int $appId
456+
* @param string|null $channel
457+
* @param int $decrement
458+
* @return PromiseInterface
459+
*/
460+
public function decrementSubscriptionsCount($appId, string $channel = null, int $increment = 1)
461+
{
462+
return $this->incrementSubscriptionsCount($appId, $channel, $increment * -1);
463+
}
464+
465+
/**
466+
* Add a channel to the set list.
467+
*
468+
* @param string|int $appId
469+
* @param string $channel
470+
* @return PromiseInterface
471+
*/
472+
public function addChannelToSet($appId, string $channel)
473+
{
474+
return $this->getPublishClient()->sadd(
475+
$this->getRedisKey($appId, null, ['channels']),
476+
$channel
477+
);
478+
}
479+
480+
/**
481+
* Remove a channel from the set list.
482+
*
483+
* @param string|int $appId
484+
* @param string $channel
485+
* @return PromiseInterface
486+
*/
487+
public function removeChannelFromSet($appId, string $channel)
488+
{
489+
return $this->getPublishClient()->srem(
490+
$this->getRedisKey($appId, null, ['channels']),
491+
$channel
492+
);
493+
}
494+
459495
/**
460496
* Set data for a topic. Might be used for the presence channels.
461497
*

0 commit comments

Comments
 (0)