Skip to content

Commit

Permalink
Merge pull request #4 from KyrneDev/analysis-M1ayDK
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
KyrneDev committed Jul 27, 2021
2 parents 6633ab6 + be4be24 commit 6813444
Show file tree
Hide file tree
Showing 75 changed files with 965 additions and 687 deletions.
57 changes: 35 additions & 22 deletions BeyondCode/src/API/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ abstract class Controller implements HttpServerInterface
/**
* Initialize the request.
*
* @param ChannelManager $channelManager
* @param ChannelManager $channelManager
*
* @return void
*/
public function __construct(ChannelManager $channelManager)
Expand All @@ -72,8 +73,9 @@ public function __construct(ChannelManager $channelManager)
/**
* Handle the opened socket connection.
*
* @param \Ratchet\ConnectionInterface $connection
* @param \Psr\Http\Message\RequestInterface $request
* @param \Ratchet\ConnectionInterface $connection
* @param \Psr\Http\Message\RequestInterface $request
*
* @return void
*/
public function onOpen(ConnectionInterface $connection, RequestInterface $request = null)
Expand All @@ -84,7 +86,7 @@ public function onOpen(ConnectionInterface $connection, RequestInterface $reques

$this->requestBuffer = (string) $request->getBody();

if (! $this->verifyContentLength()) {
if (!$this->verifyContentLength()) {
return;
}

Expand All @@ -94,15 +96,16 @@ public function onOpen(ConnectionInterface $connection, RequestInterface $reques
/**
* Handle the oncoming message and add it to buffer.
*
* @param \Ratchet\ConnectionInterface $from
* @param mixed $msg
* @param \Ratchet\ConnectionInterface $from
* @param mixed $msg
*
* @return void
*/
public function onMessage(ConnectionInterface $from, $msg)
{
$this->requestBuffer .= $msg;

if (! $this->verifyContentLength()) {
if (!$this->verifyContentLength()) {
return;
}

Expand All @@ -112,7 +115,8 @@ public function onMessage(ConnectionInterface $from, $msg)
/**
* Handle the socket closing.
*
* @param \Ratchet\ConnectionInterface $connection
* @param \Ratchet\ConnectionInterface $connection
*
* @return void
*/
public function onClose(ConnectionInterface $connection)
Expand All @@ -123,13 +127,14 @@ public function onClose(ConnectionInterface $connection)
/**
* Handle the errors.
*
* @param \Ratchet\ConnectionInterface $connection
* @param Exception $exception
* @param \Ratchet\ConnectionInterface $connection
* @param Exception $exception
*
* @return void
*/
public function onError(ConnectionInterface $connection, Exception $exception)
{
if (! $exception instanceof HttpException) {
if (!$exception instanceof HttpException) {
return;
}

Expand All @@ -145,7 +150,8 @@ public function onError(ConnectionInterface $connection, Exception $exception)
/**
* Get the content length from the headers.
*
* @param array $headers
* @param array $headers
*
* @return int
*/
protected function findContentLength(array $headers): int
Expand All @@ -168,7 +174,8 @@ protected function verifyContentLength()
/**
* Handle the oncoming connection.
*
* @param \Ratchet\ConnectionInterface $connection
* @param \Ratchet\ConnectionInterface $connection
*
* @return void
*/
protected function handleRequest(ConnectionInterface $connection)
Expand All @@ -181,7 +188,7 @@ protected function handleRequest(ConnectionInterface $connection)
$this->request->getProtocolVersion()
))->withQueryParams(QueryParameters::create($this->request)->all());

$laravelRequest = Request::createFromBase((new HttpFoundationFactory)->createRequest($serverRequest));
$laravelRequest = Request::createFromBase((new HttpFoundationFactory())->createRequest($serverRequest));

$this->ensureValidAppId($laravelRequest->get('appId'))
->ensureValidSignature($laravelRequest);
Expand All @@ -208,8 +215,9 @@ protected function handleRequest(ConnectionInterface $connection)
/**
* Send the response and close the connection.
*
* @param \Ratchet\ConnectionInterface $connection
* @param mixed $response
* @param \Ratchet\ConnectionInterface $connection
* @param mixed $response
*
* @return void
*/
protected function sendAndClose(ConnectionInterface $connection, $response)
Expand All @@ -220,13 +228,15 @@ protected function sendAndClose(ConnectionInterface $connection, $response)
/**
* Ensure app existence.
*
* @param mixed $appId
* @return $this
* @param mixed $appId
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
*
* @return $this
*/
public function ensureValidAppId($appId)
{
if (! $appId || ! $this->app = App::findById($appId)) {
if (!$appId || !$this->app = App::findById($appId)) {
throw new HttpException(401, "Unknown app id `{$appId}` provided.");
}

Expand All @@ -237,9 +247,11 @@ public function ensureValidAppId($appId)
* Ensure signature integrity coming from an
* authorized application.
*
* @param \GuzzleHttp\Psr7\ServerRequest $request
* @return $this
* @param \GuzzleHttp\Psr7\ServerRequest $request
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
*
* @return $this
*/
protected function ensureValidSignature(Request $request)
{
Expand Down Expand Up @@ -270,7 +282,8 @@ protected function ensureValidSignature(Request $request)
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\Request $request
*
* @return void
*/
abstract public function __invoke(Request $request);
Expand Down
12 changes: 7 additions & 5 deletions BeyondCode/src/API/FetchChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ class FetchChannel extends Controller
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
*/
public function __invoke(Request $request)
{
$channel = $this->channelManager->find(
$request->appId, $request->channelName
$request->appId,
$request->channelName
);

if (is_null($channel)) {
Expand All @@ -34,17 +36,17 @@ public function __invoke(Request $request)
->getChannelsMembersCount($request->appId, [$request->channelName])
->then(function ($channelMembers) use ($connectionsCount, $request) {
return [
'occupied' => $connectionsCount > 0,
'occupied' => $connectionsCount > 0,
'subscription_count' => $connectionsCount,
'user_count' => $channelMembers[$request->channelName] ?? 0,
'user_count' => $channelMembers[$request->channelName] ?? 0,
];
});
}

// For the rest of the channels, we might as well
// send the basic response with the subscriptions count.
return [
'occupied' => $connectionsCount > 0,
'occupied' => $connectionsCount > 0,
'subscription_count' => $connectionsCount,
];
});
Expand Down
9 changes: 5 additions & 4 deletions BeyondCode/src/API/FetchChannels.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class FetchChannels extends Controller
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
*/
public function __invoke(Request $request)
Expand All @@ -23,7 +24,7 @@ public function __invoke(Request $request)
if ($request->has('info')) {
$attributes = explode(',', trim($request->info));

if (in_array('user_count', $attributes) && ! Str::startsWith($request->filter_by_prefix, 'presence-')) {
if (in_array('user_count', $attributes) && !Str::startsWith($request->filter_by_prefix, 'presence-')) {
throw new HttpException(400, 'Request must be limited to presence channels in order to fetch user_count');
}
}
Expand Down Expand Up @@ -53,7 +54,7 @@ public function __invoke(Request $request)
->getChannelsMembersCount($request->appId, $channelNames)
->then(function ($counts) use ($channels, $attributes) {
$channels = $channels->map(function ($channel) use ($counts, $attributes) {
$info = new stdClass;
$info = new stdClass();

$channelName = $channel instanceof Channel
? $channel->getName()
Expand All @@ -69,7 +70,7 @@ public function __invoke(Request $request)
})->all();

return [
'channels' => $channels ?: new stdClass,
'channels' => $channels ?: new stdClass(),
];
});
});
Expand Down
5 changes: 3 additions & 2 deletions BeyondCode/src/API/FetchUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ class FetchUsers extends Controller
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
*/
public function __invoke(Request $request)
{
if (! Str::startsWith($request->channelName, 'presence-')) {
if (!Str::startsWith($request->channelName, 'presence-')) {
return new HttpException(400, "Invalid presence channel `{$request->channelName}`");
}

Expand Down
17 changes: 11 additions & 6 deletions BeyondCode/src/API/TriggerEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class TriggerEvent extends Controller
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
*/
public function __invoke(Request $request)
Expand All @@ -28,13 +29,14 @@ public function __invoke(Request $request)
// then the message simply will get broadcasted
// across the other servers.
$channel = $this->channelManager->find(
$request->appId, $channelName
$request->appId,
$channelName
);

$payload = [
'event' => $request->name,
'event' => $request->name,
'channel' => $channelName,
'data' => $request->data,
'data' => $request->data,
];

if ($channel) {
Expand All @@ -46,15 +48,18 @@ public function __invoke(Request $request)
}

$this->channelManager->broadcastAcrossServers(
$request->appId, $request->socket_id, $channelName, (object) $payload
$request->appId,
$request->socket_id,
$channelName,
(object) $payload
);

if ($this->app->statisticsEnabled) {
StatisticsCollector::apiMessage($request->appId);
}

DashboardLogger::log($request->appId, DashboardLogger::TYPE_API_MESSAGE, [
'event' => $request->name,
'event' => $request->name,
'channel' => $channelName,
'payload' => $request->data,
]);
Expand Down
Loading

0 comments on commit 6813444

Please sign in to comment.