<commit_before><?php

namespace Marks\Stockfighter\WebSocket;

use Devristo\Phpws\Messaging\WebSocketMessageInterface;
use Marks\Stockfighter\Contracts\WebSocketContract;
use Marks\Stockfighter\Stockfighter;
use Zend\Log\Logger;
use Zend\Log\LoggerInterface;
use Zend\Log\Writer\Stream;

class WebSocket implements WebSocketContract
{
	/**
	 * The URL to the websocket endpoint.
	 * @var string
	 */
	protected $url = '';

	/**
	 * The websocket client.
	 * @var \Devristo\Phpws\Client\WebSocket
	 */
	protected $client = null;

	/**
	 * The Stockfighter instance.
	 * @var Stockfighter
	 */
	protected $stockfighter = null;

	/**
	 * The Zend logger.
	 * @var LoggerInterface
	 */
	protected $logger = null;

	public function __construct($url, Stockfighter $stockfighter)
	{
		$this->url = $url;
		$this->stockfighter = $stockfighter;

		// Create the client (and a logger because the library requires it).
		// Well, we might as well embrace the logger since we have to use it...
		$this->logger = new Logger();
		$writer = new Stream('php://output');
		$this->logger->addWriter($writer);
		$this->client = new \Devristo\Phpws\Client\WebSocket($this->url, $this->stockfighter->loop,
			$this->logger);
	}

	public function connect()
	{
		$this->client->open();
	}

	public function receive(callable $callback)
	{
		$this->client->on('request', function () {
			$this->logger->notice('Request object created!');
		});

		$this->client->on('handshake', function () {
			$this->logger->notice('Handshake received!');
		});

		$this->client->on('connect', function () {
			$this->logger->notice('Connected!');
		});

		$this->client->on('message', function (WebSocketMessageInterface $message) use ($callback) {

			// Wait for the message to be finalized.
			if (!$message->isFinalised()) {
				return;
			}

			// Get the contents.
			$contents = json_decode($message, true);
			if (!$contents) return;

			// Handle the contents.
			$value = $this->handleContents($contents);

			// Call the callback with the value.
			$result = $callback($value);

			// If the callback returns true, close the connection.
			if ($result === true) {
				$this->client->close();
			}<commit_msg>I need to go to bed.<commit_after><?php

namespace Marks\Stockfighter\WebSocket;

use Devristo\Phpws\Messaging\WebSocketMessageInterface;
use Marks\Stockfighter\Contracts\WebSocketContract;
use Marks\Stockfighter\Stockfighter;
use Zend\Log\Logger;
use Zend\Log\LoggerInterface;
use Zend\Log\Writer\Stream;

class WebSocket implements WebSocketContract
{
	/**
	 * The URL to the websocket endpoint.
	 * @var string
	 */
	protected $url = '';

	/**
	 * The websocket client.
	 * @var \Devristo\Phpws\Client\WebSocket
	 */
	protected $client = null;

	/**
	 * The Stockfighter instance.
	 * @var Stockfighter
	 */
	protected $stockfighter = null;

	/**
	 * The Zend logger.
	 * @var LoggerInterface
	 */
	protected $logger = null;

	public function __construct($url, Stockfighter $stockfighter)
	{
		$this->url = $url;
		$this->stockfighter = $stockfighter;

		// Create the client (and a logger because the library requires it).
		// Well, we might as well embrace the logger since we have to use it...
		$this->logger = new Logger();
		$writer = new Stream('php://output');
		$this->logger->addWriter($writer);
		$this->client = new \Devristo\Phpws\Client\WebSocket($this->url, $this->stockfighter->loop,
			$this->logger);
	}

	public function connect()
	{
		$this->client->open();
	}

	public function receive(callable $callback)
	{
		$this->client->on('request', function () {
			$this->logger->notice('Request object created!');
		});

		$this->client->on('handshake', function () {
			$this->logger->notice('Handshake received!');
		});

		$this->client->on('connect', function () {
			$this->logger->notice('Connected!');
		});

		$this->client->on('message', function (WebSocketMessageInterface $message) use ($callback) {

			// Wait for the message to be finalized.
			if (!$message->isFinalised()) {
				return;
			}

			// Get the contents.
			$contents = json_decode($message->getData(), true);
			if (!$contents) return;

			// Handle the contents.
			$value = $this->handleContents($contents);

			// Call the callback with the value.
			$result = $callback($value);

			// If the callback returns true, close the connection.
			if ($result === true) {
				$this->client->close();
			}
{'fraction_non_alphanumeric': 0.1587482868889904, 'fraction_numerical': 0.0, 'mean_word_length': 3.6585106382978725, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 64, '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': '940485', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1347, 'n_words': 343}