<commit_before><?php namespace J42\LaravelFirebase;

use \Illuminate\Database\Eloquent\Collection;

class FirebaseClient {

	# Properties
	private $http;
	private $timeout;
	private $token;
	private $host;

	private $passthrough = ['set','push','update','get'];


	// Return: (obj) FirebaseClient
	// Args: (Array) $config
	public function __construct(Array $config) {

		// Valid host?
		if (empty($config['host'])) throw new \UnexpectedValueException('Please enter a valid Firebase host URL.');
		if (strpos($config['host'], 'http://') > -1) throw new \UnexpectedValueException('Please use HTTPS for all Firebase URLs.');

		// Set Host URI
		$this->setHost($config['host']);

		// Set Secret
		$this->setToken($config['token'] ?: null);

		// Set Timeout
		$this->setTimeout($config['timeout'] ?: 10);

		// Set Client
		$this->http = new \GuzzleHttp\Client();

	}


	// Return: (json) Firebase Response
	// Args: (string) $path, (mixed) $data
	public function __call($func, $args) {

		// Errors
		if (!in_array($func, $this->passthrough)) throw new \UnexpectedValueException('Unexpected method called');
		if (count($args) < 1) throw new \UnexpectedValueException('Not enough arguments');

		// Write Methods
		switch ($func) {
			case 'set': $requestType = 'PUT'; break;
			case 'push': $requestType = 'POST'; break;
			case 'update': $requestType = 'PATCH'; break;
			case 'get': $requestType = 'GET'; break;
		}

		// Process URL/Path
		$url = (strpos($args[0], 'https://') === false) ? $this->absolutePath($args[0]) : $args[0];

		if ($func !== 'get') {
			// Write Data
			return $this->write($url, $args[1], $requestType);
		} else {
			// Read Data
			return $this->getJson($url, (isset($args[1]) ? $args[1] : false));
		}

	}


	// Return: (Guzzle) Firebase Response
	// Args: (string) $path
	public function getJson($path, $eloquentCollection = false) {

		// Process Request
		$request  = $this->http->createRequest('GET', $path);
		$response = $this->http->send($request);
		$response = $this->validateResponse($response)->json();

		// Is Response Valid?
		return ($eloquentCollection) ? $this->makeCollection($response, $eloquentCollection) : $response;
	}


	// Return: (Illuminate\Database\Eloquent\Collection) Eloquent Collection
	// Args: (Array) $response, (string) $eloquentModel
	public function makeCollection(Array $response, $eloquentModel) {
		// Sanity Check
		if (!class_exists($eloquentModel)) return Collection::make($response);
		// Get IDs
		$ids = [];
		foreach ($response as $id => $object) {
			$ids[] = $id;
			if (isset($object['_id'])) $ids[] = $object['_id'];
			if (isset($object['id'])) $ids[] = $object['id'];
		}
		// Return Collection
		return call_user_func_array($eloquentModel.'::whereIn', ['_id', $ids]);
	}


	// Return: (Guzzle) Firebase Response
	// Args: (string) $path, (Array) $data, (string) $method
	public function write($path, Array $data, $method = 'PUT') {

		// Sanity Checks
		$json = json_encode($data);
		if ($json === 'null') throw new \UnexpectedValueException('HTTP Error: Invalid request (invalid JSON)');

		// Process Request
		$request  = $this->http->createRequest($method, $path, ['json' => $json]);
		$request->setHeader('Content-Type', 'application/json');
		$request->setHeader('Content-Length', strlen($json));
		$response = $this->http->send($request);<commit_msg>Added delete method, minor refactor<commit_after><?php namespace J42\LaravelFirebase;

use \Illuminate\Database\Eloquent\Collection;

class FirebaseClient {

	# Properties
	private $http;
	private $timeout;
	private $token;
	private $host;

	private $passthrough = ['set','push','update','get'];


	// Return: (obj) FirebaseClient
	// Args: (Array) $config
	public function __construct(Array $config) {

		// Valid host?
		if (empty($config['host'])) throw new \UnexpectedValueException('Please enter a valid Firebase host URL.');
		if (strpos($config['host'], 'http://') > -1) throw new \UnexpectedValueException('Please use HTTPS for all Firebase URLs.');

		// Set Host URI
		$this->setHost($config['host']);

		// Set Secret
		$this->setToken($config['token'] ?: null);

		// Set Timeout
		$this->setTimeout($config['timeout'] ?: 10);

		// Set Client
		$this->http = new \GuzzleHttp\Client();

	}


	// Return: (json) Firebase Response
	// Args: (string) $path, (mixed) $data
	public function __call($func, $args) {

		// Errors
		if (!in_array($func, $this->passthrough)) throw new \UnexpectedValueException('Unexpected method called');
		if (count($args) < 1) throw new \UnexpectedValueException('Not enough arguments');

		// Process URL/Path
		$url = (strpos($args[0], 'https://') === false) ? $this->absolutePath($args[0]) : $args[0];

		// Write Methods
		switch ($func) {

			case 'get':
				// Read Data 
				$requestType = 'GET'; 
				return $this->read($url, (isset($args[1]) ? $args[1] : false));
			break;

			case 'delete':
				// Delete Data
				$requestType = 'DELETE';
				return $this->delete($url);
			break;

			case 'set': $requestType = 'PUT'; break;
			case 'push': $requestType = 'POST'; break;
			case 'update': $requestType = 'PATCH'; break;
		}

		// Else Write Data
		return ($requestType) ? $this->write($url, $args[1], $requestType) : null;

	}


	// Return: (Guzzle) Firebase Response
	// Args: (string) $path
	public function delete($path) {

		// Process Request
		$request  = $this->http->createRequest('DELETE', $path);
		$response = $this->http->send($request);
		return $this->validateResponse($response)->json();
	}


	// Return: (Guzzle) Firebase Response
	// Args: (string) $path
	public function read($path, $eloquentCollection = false) {

		// Process Request
		$request  = $this->http->createRequest('GET', $path);
		$response = $this->http->send($request);
		$response = $this->validateResponse($response)->json();

		// Is Response Valid?
		return ($eloquentCollection) ? $this->makeCollection($response, $eloquentCollection) : $response;
	}


	// Return: (Illuminate\Database\Eloquent\Collection) Eloquent Collection
	// Args: (Array) $response, (string) $eloquentModel
	public function makeCollection(Array $response, $eloquentModel) {

		// Sanity Check
		if (!class_exists($eloquentModel)) return Collection::make($response);

		// Get IDs
		$ids = [];
		foreach ($response as $id => $object) {
			$ids[] = $id;
			if (isset($object['_id'])) $ids[] = $object['_id'];
			if (isset($object['id'])) $ids[] = $object['id'];
		}

		// Return Collection
		return call_user_func_array($eloquentModel.'::whereIn', ['_id', $ids]);
	}


	// Return: (Guzzle) Firebase Response
	// Args: (string) $path, (Array) $data, (string) $method
	public function write($path, Array $data, $method = 'PUT') {

		// Sanity Checks
		$json = json_encode($data);
		if ($json === 'null') throw new \UnexpectedValueException('HTTP Error: Invalid request (invalid JSON)');

		// Process Request
		$request  = $this->http->createRequest($method, $path, ['json' => $json]);
		$request->setHeader('Content-Type', 'application/json');
		$request->setHeader('Content-Length', strlen($json));
		$response = $this->http->send($request);
{'fraction_non_alphanumeric': 0.18937579258841764, 'fraction_numerical': 0.0033817105819360293, 'mean_word_length': 4.242245199409158, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 58, 'https://': 2, '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': '922020', 'n_tokens_mistral': 2493, 'n_tokens_neox': 2275, 'n_words': 570}