<commit_before><?php

namespace LaraComponents\Centrifuge;

use Exception;
use Illuminate\Broadcasting\BroadcastException;
use LaraComponents\Centrifuge\Contracts\Centrifuge;
use Illuminate\Broadcasting\Broadcasters\Broadcaster;
use Symfony\Component\HttpKernel\Exception\HttpException;

class CentrifugeBroadcaster extends Broadcaster
{
    /**
     * The Centrifuge SDK instance.
     *
     * @var \LaraComponents\Centrifuge\Contracts\Centrifuge
     */
    protected $centrifuge;

    /**
     * Create a new broadcaster instance.
     *
     * @param  \LaraComponents\Centrifuge\Contracts\Centrifuge  $centrifuge
     * @return void
     */
    public function __construct(Centrifuge $centrifuge)
    {
        $this->centrifuge = $centrifuge;
    }

    /**
     * Authenticate the incoming request for a given channel.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return mixed
     */
    public function auth($request)
    {
        if ($request->user()) {
            $client = $request->get('client', '');
            $channels = $request->get('channels', []);
            $channels = is_array($channels) ? $channels : [$channels];

            $response = [];
            $info = json_encode([]);
            foreach ($channels as $channel) {
                try {
                    $result = parent::verifyUserCanAccessChannel($request, $channel);
                } catch (HttpException $e) {
                    $result = false;
                }

                $response[$channel] = $result ? [
                    'sign' => $this->centrifuge->generateToken($client, $channel, $info),
                    'info' => $info,
                ] : [
                    'status' => 403,
                ];
            }

            return response()->json($response);
        } else {
            throw new HttpException(401);
        }
    }

    /**
     * Return the valid authentication response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $result
     * @return mixed
     */
    public function validAuthenticationResponse($request, $result)
    {
        return $result;
    }

    /**
     * Broadcast the given event.
     *
     * @param  array  $channels
     * @param  string  $event
     * @param  array  $payload
     * @return void
     */
    public function broadcast(array $channels, $event, array $payload = [])
    {
        $payload['event'] = $event;

        try {
            $response = $this->centrifuge->broadcast($this->formatChannels($channels), $payload);
        } catch (Exception $e) {
            throw new BroadcastException($e->getMessage());
        }
    }

    /**
     * Get the Centrifuge instance.
     *
     * @return \LaraComponents\Centrifuge\Contracts\Centrifuge
     */
    public function getCentrifuge()
    {
        return $this->centrifuge;
    }
}<commit_msg>Fix issue #4 and other small fixes<commit_after><?php

namespace LaraComponents\Centrifuge;

use Exception;
use Illuminate\Broadcasting\BroadcastException;
use Illuminate\Broadcasting\Broadcasters\Broadcaster;
use Symfony\Component\HttpKernel\Exception\HttpException;
use LaraComponents\Centrifuge\Contracts\Centrifuge as CentrifugeContract;

class CentrifugeBroadcaster extends Broadcaster
{
    /**
     * The Centrifuge SDK instance.
     *
     * @var \LaraComponents\Centrifuge\Contracts\Centrifuge
     */
    protected $centrifuge;

    /**
     * Create a new broadcaster instance.
     *
     * @param  \LaraComponents\Centrifuge\Contracts\Centrifuge  $centrifuge
     */
    public function __construct(CentrifugeContract $centrifuge)
    {
        $this->centrifuge = $centrifuge;
    }

    /**
     * Authenticate the incoming request for a given channel.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return mixed
     */
    public function auth($request)
    {
        if ($request->user()) {
            $client = $request->get('client', '');
            $channels = $request->get('channels', []);
            $channels = is_array($channels) ? $channels : [$channels];

            $response = [];
            $info = json_encode([]);
            foreach ($channels as $channel) {
                try {
                    $result = $this->verifyUserCanAccessChannel($request, $channel);
                } catch (HttpException $e) {
                    $result = false;
                }

                $response[$channel] = $result ? [
                    'sign' => $this->centrifuge->generateToken($client, $channel, $info),
                    'info' => $info,
                ] : [
                    'status' => 403,
                ];
            }

            return response()->json($response);
        } else {
            throw new HttpException(401);
        }
    }

    /**
     * Return the valid authentication response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $result
     * @return mixed
     */
    public function validAuthenticationResponse($request, $result)
    {
        return $result;
    }

    /**
     * Broadcast the given event.
     *
     * @param  array  $channels
     * @param  string  $event
     * @param  array  $payload
     * @return void
     */
    public function broadcast(array $channels, $event, array $payload = [])
    {
        $payload['event'] = $event;

        $response = $this->centrifuge->broadcast($this->formatChannels($channels), $payload);

        if ((is_array($response) && is_null($response['error'])) {
            return;
        }

        throw new BroadcastException($response['error']);
    }

    /**
     * Get the Centrifuge instance.
     *
     * @return \LaraComponents\Centrifuge\Contracts\Centrifuge
     */
    public function getCentrifuge()
    {
        return $this->centrifuge;
    }
}
{'fraction_non_alphanumeric': 0.12264150943396226, 'fraction_numerical': 0.0022298456260720413, 'mean_word_length': 1.947927199191102, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6513985', 'n_tokens_mistral': 1658, 'n_tokens_neox': 1606, 'n_words': 332}