<commit_before><?php

namespace Thruway;



use React\EventLoop\LoopInterface;
use Thruway\Message\AuthenticateMessage;
use Thruway\Message\ChallengeMessage;
use Thruway\Peer\Client;
use Thruway\Transport\PawlTransportProvider;
use Thruway\Transport\TransportInterface;
use Evenement\EventEmitterInterface;
use Evenement\EventEmitterTrait;


/**
 * Class Connection
 *
 * @package Thruway
 */
class Connection implements EventEmitterInterface
{

    /**
     * Using \Evenement\EventEmitterTrait to implements \Evenement\EventEmitterInterface
     * @see \Evenement\EventEmitterTrait
     */
    use EventEmitterTrait;

    /**
     * @var \Thruway\Peer\Client
     */
    private $client;

    /**
     * @var \Thruway\Transport\TransportInterface
     */
    private $transport;

    /**
     * @var array
     */
    private $options;

    /**
     * Constructor
     *
     * @param array $options
     * @param \React\EventLoop\LoopInterface $loop
     * @throws \Exception
     */
    public function __construct(Array $options, LoopInterface $loop = null)
    {

        $this->options = $options;
        $this->client  = new Client($options['realm'], $loop);
        $url           = isset($options['url']) ? $options['url'] : null;
        $pawlTransport = new PawlTransportProvider($url);
        $this->client->addTransportProvider($pawlTransport);
        $this->client->setReconnectOptions($options);

        /*
         * Authentication on challenge callback
         */
        if (isset($options['onChallenge']) && is_callable($options['onChallenge'])
            && isset($options['authmethods'])
            && is_array($options['authmethods'])
        ) {
            $this->client->setAuthMethods($options['authmethods']);
            $this->client->on(
                'challenge',
                function (ClientSession $session, ChallengeMessage $msg) use ($options) {
                    $token = $options['onChallenge']($session, $msg->getAuthMethod());
                    $session->sendMessage(new AuthenticateMessage($token));
                }
            );
        }

        if (isset($this->options['onClose']) && is_callable($this->options['onClose'])) {
            $this->on('close', $this->options['onClose']);
        }

        /*
         * Handle On Open event
         *
         */
        $this->client->on(
            'open',
            function (ClientSession $session, TransportInterface $transport) {
                $this->transport = $transport;
                $this->emit('open', [$session]);
            }
        );

        /*
         * Handle On Close event
         */
        $this->client->on(
            'close',
            function ($reason) {<commit_msg>Changes for wamp-cra support in Connection<commit_after><?php

namespace Thruway;


use React\EventLoop\LoopInterface;
use Thruway\Message\AuthenticateMessage;
use Thruway\Message\ChallengeMessage;
use Thruway\Peer\Client;
use Thruway\Transport\PawlTransportProvider;
use Thruway\Transport\TransportInterface;
use Evenement\EventEmitterInterface;
use Evenement\EventEmitterTrait;


/**
 * Class Connection
 *
 * @package Thruway
 */
class Connection implements EventEmitterInterface
{

    /**
     * Using \Evenement\EventEmitterTrait to implements \Evenement\EventEmitterInterface
     * @see \Evenement\EventEmitterTrait
     */
    use EventEmitterTrait;

    /**
     * @var \Thruway\Peer\Client
     */
    private $client;

    /**
     * @var \Thruway\Transport\TransportInterface
     */
    private $transport;

    /**
     * @var array
     */
    private $options;

    /**
     * Constructor
     *
     * @param array $options
     * @param \React\EventLoop\LoopInterface $loop
     * @throws \Exception
     */
    public function __construct(Array $options, LoopInterface $loop = null)
    {

        $this->options = $options;
        $this->client  = new Client($options['realm'], $loop);
        $url           = isset($options['url']) ? $options['url'] : null;
        $pawlTransport = new PawlTransportProvider($url);
        $this->client->addTransportProvider($pawlTransport);
        $this->client->setReconnectOptions($options);

        /*
         * Authentication on challenge callback
         */
        if (isset($options['onChallenge']) && is_callable($options['onChallenge'])
            && isset($options['authmethods'])
            && is_array($options['authmethods'])
        ) {
            $this->client->setAuthMethods($options['authmethods']);
            $this->client->on(
                'challenge',
                function (ClientSession $session, ChallengeMessage $msg) use ($options) {
                    $token = call_user_func_array($options['onChallenge'], [$session, $msg->getAuthMethod(), $msg]);
                    $session->sendMessage(new AuthenticateMessage($token));
                }
            );
        }

        //Set Authid
        if (isset($options['authid'])) {
            $this->client->setAuthId($options['authid']);
        }

        if (isset($this->options['onClose']) && is_callable($this->options['onClose'])) {
            $this->on('close', $this->options['onClose']);
        }

        /*
         * Handle On Open event
         *
         */
        $this->client->on(
            'open',
            function (ClientSession $session, TransportInterface $transport) {
                $this->transport = $transport;
                $this->emit('open', [$session]);
            }
        );

        /*
         * Handle On Close event
         */
        $this->client->on(
            'close',
            function ($reason) {
{'fraction_non_alphanumeric': 0.13908107149192833, 'fraction_numerical': 0.0, 'mean_word_length': 2.163860830527497, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, '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': '1330801', 'n_tokens_mistral': 1566, 'n_tokens_neox': 1530, 'n_words': 274}