<?php
namespace ModulesAbstract\FluxRest\Response;

// TODO: muta sesiunea in storage
// TODO: trebuie sa  poti reseta flux si in updater
class Main
{
	public $operations= [];

	public $responseHandling = "default";

	/**
	 * Append an operation
	 * @return \ModulesAbstract\FluxRest\Response\Main
	 */
	public function appendOperation($operation)
	{
		$this->operations[]= $operation;
		return $this;
	}

	/**
	 * Clear the internal operations queue
	 * @return \ModulesAbstract\FluxRest\Response\Main
	 */
	public function clearOperations()
	{
		$this->operations = [];
		return $this;
	}

	/**
	 * Check if there are any operations present
	 * @return boolean
	 */
	public function hasOperations()
	{
		return (bool)$this->operations;
	}

	/**
	 * Check if a certain operation is present in the file
	 * @return array
	 */
	public function filterOperations($callback)
	{
		return array_filter($this->operations, $callback);
	}

	/**
	 * Check if a certain operation is present in the file
	 * @return boolean
	 */
	public function hasOperation($operationId)
	{
		return (bool)$this->countOperation($operationId);
	}

	/**
	 * Count the number of times an operation is present
	 */
	public function countOperation($operationId)
	{
		return count($this->filterOperations(function(\ModulesAbstract\FluxRest\Response\Operation\OperationAbstract $op) use($operationId) {
			return $operationId == $op->action;
		}));
	}
}
