Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jan 3, 2018
1 parent 66d1ffd commit 434b348
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
47 changes: 32 additions & 15 deletions src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Broadcasting\Broadcasters;

use Exception;
use ReflectionClass;
use ReflectionFunction;
use Illuminate\Support\Str;
use Illuminate\Container\Container;
Expand Down Expand Up @@ -56,6 +58,7 @@ protected function verifyUserCanAccessChannel($request, $channel)
}

$parameters = $this->extractAuthParameters($pattern, $channel, $callback);

$handler = $this->normalizeChannelHandlerToCallable($callback);

if ($result = $handler($request->user(), ...$parameters)) {
Expand All @@ -71,7 +74,7 @@ protected function verifyUserCanAccessChannel($request, $channel)
*
* @param string $pattern
* @param string $channel
* @param callable $callback
* @param callable|string $callback
* @return array
*/
protected function extractAuthParameters($pattern, $channel, $callback)
Expand All @@ -88,23 +91,37 @@ protected function extractAuthParameters($pattern, $channel, $callback)
/**
* Extracts the parameters out of what the user passed to handle the channel authentication.
*
* @param callable|string $callback
* @param callable|string $callback
* @return \ReflectionParameter[]
* @throws \Exception
*/
protected function extractParameters($callback)
{
if (is_callable($callback)) {
return (new ReflectionFunction($callback))->getParameters();
} elseif (is_string($callback)) {
return $this->extractParametersFromClass($callback);
}

if (is_string($callback)) {
return (new \ReflectionClass($callback))
->getMethod('join')
->getParameters();
throw new Exception('Given channel handler is an unknown type.');
}

/**
* Extracts the parameters out of a class channel's "join" method.
*
* @param string $callback
* @return \ReflectionParameter[]
* @throws \Exception
*/
protected function extractParametersFromClass($callback)
{
$reflection = new ReflectionClass($callback);

if (! $reflection->hasMethod('join')) {
throw new Exception('Class based channel must define a "join" method.');
}

throw new \Exception('Unknown channel handler type.');
return $reflection->getMethod('join')->getParameters();
}

/**
Expand Down Expand Up @@ -226,17 +243,17 @@ protected function binder()
}

/**
* @param $callback
* Normalize the given callback into a callable.
*
* @param mixed $callback
* @return callable|\Closure
*/
protected function normalizeChannelHandlerToCallable($callback)
{
return is_callable($callback)
? $callback
: function (...$args) use ($callback) {
return Container::getInstance()
->make($callback)
->join(...$args);
};
return is_callable($callback) ? $callback : function (...$args) use ($callback) {
return Container::getInstance()
->make($callback)
->join(...$args);
};
}
}
1 change: 0 additions & 1 deletion tests/Broadcasting/BroadcasterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public function testCanUseChannelClasses()

/**
* @expectedException \Exception
* @expectedExceptionMessage Unknown channel handler type.
*/
public function testUnknownChannelAuthHandlerTypeThrowsException()
{
Expand Down

0 comments on commit 434b348

Please sign in to comment.