From 434b348c5dda1b04486ca6134671d83046bd5c96 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 3 Jan 2018 10:10:25 -0600 Subject: [PATCH] formatting --- .../Broadcasting/Broadcasters/Broadcaster.php | 47 +++++++++++++------ tests/Broadcasting/BroadcasterTest.php | 1 - 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php b/src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php index 6b71b34cfb76..18a8fefbdd8e 100644 --- a/src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php +++ b/src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php @@ -2,6 +2,8 @@ namespace Illuminate\Broadcasting\Broadcasters; +use Exception; +use ReflectionClass; use ReflectionFunction; use Illuminate\Support\Str; use Illuminate\Container\Container; @@ -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)) { @@ -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) @@ -88,7 +91,7 @@ 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 */ @@ -96,15 +99,29 @@ 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(); } /** @@ -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); + }; } } diff --git a/tests/Broadcasting/BroadcasterTest.php b/tests/Broadcasting/BroadcasterTest.php index c1364ad40f05..a9b46388a54e 100644 --- a/tests/Broadcasting/BroadcasterTest.php +++ b/tests/Broadcasting/BroadcasterTest.php @@ -67,7 +67,6 @@ public function testCanUseChannelClasses() /** * @expectedException \Exception - * @expectedExceptionMessage Unknown channel handler type. */ public function testUnknownChannelAuthHandlerTypeThrowsException() {