Skip to content

Commit

Permalink
feat: update doc blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
chipslays committed Feb 7, 2023
1 parent d3eee89 commit d45686e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 31 deletions.
5 changes: 5 additions & 0 deletions src/Connection/Channels.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ class Channels
/** @var string[] */
protected array $channels = [];

/**
* Constructor.
*
* @param TcpConnection|Connection $connection
*/
public function __construct(protected TcpConnection|Connection $connection)
{
//
Expand Down
43 changes: 26 additions & 17 deletions src/Connections.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function get(int $id, mixed $default = null): ?Connection
/**
* Get specific connections.
*
* @param array|integer $ids
* @param int[]|int $ids
* @return static
*/
public function only(array|int $ids): static
Expand Down Expand Up @@ -143,7 +143,7 @@ public function add(TcpConnection|Connection $connection): self
/**
* Remove connection from collection.
*
* @param TcpConnection|Connection|integer $connection
* @param TcpConnection|Connection|int $connection
* @return self
*/
public function remove(TcpConnection|Connection|int $connection): self
Expand All @@ -166,7 +166,7 @@ public function first(): ?Connection
}

/**
* Get last connection from connection.
* Get last connection in this collection.
*
* @return Connection|null
*/
Expand All @@ -176,6 +176,8 @@ public function last(): ?Connection
}

/**
* Get a limited collection of connections.
*
* @param int $count
* @param int $offset
* @return static
Expand All @@ -186,6 +188,8 @@ public function limit(int $count, int $offset = 0): static
}

/**
* Get a filtered collection of connections.
*
* @param callable|null $callback
* @return static
*/
Expand All @@ -195,6 +199,8 @@ public function filter(callable $callback): static
}

/**
* Map each connection in collection.
*
* @param callable $callback
* @return static
*/
Expand All @@ -208,6 +214,8 @@ public function map(callable $callback): static
}

/**
* Do something on over each connection in collection.
*
* @param callable $callback
* @return self
*/
Expand All @@ -223,6 +231,8 @@ public function each(callable $callback): self
}

/**
* Get a empty collection of connections.
*
* @return static
*/
public function clear(): static
Expand All @@ -231,14 +241,18 @@ public function clear(): static
}

/**
* @return Connection
* Get a first connection and remove this from collection.
*
* @return Connection|null
*/
public function shift(): Connection
public function shift(): ?Connection
{
return array_shift($this->connections);
}

/**
* Split a collection to array of chunks (each chunk is a collection of connections).
*
* @param int $size
* @return array
*/
Expand Down Expand Up @@ -269,21 +283,16 @@ public function tap(callable $callback): self
}

/**
* @param callable $callback
* @return mixed
*/
public function pipe(callable $callback): mixed
{
return call_user_func($callback, $this);
}

/**
* Get random connection.
* Get a random connection.
*
* @return Connection
* @return Connection|null
*/
public function random(): Connection
public function random(): ?Connection
{
if ($this->count() == 0) {
return null;
}

return $this->connections[array_rand($this->connections)];
}
}
26 changes: 14 additions & 12 deletions src/Events/AbstractEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ abstract class AbstractEvent
public readonly Payload $payload;

/**
* Available if client passed `channelId`.
* Available if client passed `channel_id_`.
*
* @var Channel|null
*/
public readonly ?Channel $channel;

/**
* Available if client passed `targetId`.
* Available if client passed `target_id`.
*
* @var Connection|null
*/
Expand Down Expand Up @@ -85,23 +85,25 @@ public function boot(Connection $connection, Payload $payload): self
$this->server = Server::getInstance();
$this->data = &$this->payload->data;

$this->initMagicalVars();
$this->setMagicalVariables();

return $this;
}

/**
* @return void
*/
protected function initMagicalVars(): void
protected function setMagicalVariables(): void
{
// Get channel instance by `channelId` parameter.
$this->channel = $this->server->channel($this->payload('channelId', ''));
// Get channel instance by `channel_id_` parameter.
if ($this->data->has('channel_id')) {
$this->channel = $this->server->channel($this->payload('channel_id'));
}

// Get target connection instance by `targetId` parameter.
$this->target = $this->payload->has('targetId')
? $this->server->connection((int) $this->payload('targetId'))
: null;
// Get target connection instance by `target_id` parameter.
if ($this->data->has('target_id')) {
$this->target = $this->server->connection((int) $this->payload('target_id'));
}
}

/**
Expand Down Expand Up @@ -163,7 +165,7 @@ public function broadcast(string $event, array $data = [], array|TcpConnection|C
}

/**
* Getter for channel (available if client passed `channelId`).
* Getter for channel (available if client passed `channel_id_`).
*
* @return Channel|null
*/
Expand All @@ -173,7 +175,7 @@ public function channel(): ?Channel
}

/**
* Getter for target (available if client passed `targetId`).
* Getter for target (available if client passed `target_id`).
*
* @return Connection|null
*/
Expand Down
2 changes: 0 additions & 2 deletions src/Events/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
class Event extends AbstractEvent
{
/**
* This method here is useless, but needed.
*
* @return void
*/
public function handle(Connection $connection, Payload $payload, Server $server): void
Expand Down
6 changes: 6 additions & 0 deletions src/Traits/Rawable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ trait Rawable
{
protected $onRawHandler;

/**
* Handle raw data from client.
*
* @param callable $handler
* @return void
*/
public function onRaw(callable $handler): void
{
$this->onRawHandler = $handler;
Expand Down

0 comments on commit d45686e

Please sign in to comment.