<commit_before><commit_msg>Старт Рефакторинга компонента Коммуникаций, в погоне за Интегратором (cherry picked from commit 6e985dd)<commit_after><?php
/**
 * Created by <NAME> <<EMAIL>>.
 * Author: <NAME> <<EMAIL>>
 * Project: jungle
 * IDE: PhpStorm
 * Date: 02.10.2016
 * Time: 23:38
 */
namespace Jungle\Util\Communication {

	use Jungle\Util\Communication\Sequence\CommandInterface;
	use Jungle\Util\Communication\Sequence\ProcessSequence;
	use Jungle\Util\Communication\Sequence\ProcessSequenceInterface;
	use Jungle\Util\Communication\Sequence\SpecificationInterface;

	/**
	 * Class SequenceTrait
	 * @package Jungle\Util\Communication
	 */
	trait SequenceTrait{

		/** @var  array */
		protected $sequence = [];

		/** @var  SpecificationInterface */
		protected $specification;

		/**
		 * @param array $sequence
		 * @throws Exception
		 */
		public function setSequence(array $sequence){
			foreach($sequence as & $item){
				$item = array_replace([
					'name'          => null,
					'params'        => null,
					'aggregation'   => null,
				],$item);
				if(!isset($item['aggregation']) || !isset($item['params'])){
					$item['params'] = (array)$item['params'];
				}
				$item['command'] = $this->specification->getCommand($item['name']);
				if(!$item['command']){
					throw new Exception('Not found command with name "'.$item['name'].'"');
				}

			}
			$this->sequence = $sequence;
		}

		/**
		 * @param SpecificationInterface $specification
		 * @return $this
		 */
		public function setSpecification(SpecificationInterface $specification){
			$this->specification = $specification;
			return $this;
		}

		/**
		 * @return SpecificationInterface
		 */
		public function getSpecification(){
			return $this->specification;
		}

		/**
		 * @return ProcessSequenceInterface
		 */
		public function run(){
			$processSequence = new ProcessSequence($this);
			foreach($this->sequence as $item){
				/** @var CommandInterface $command */
				$command = $item['command'];
				if(isset($item['aggregation'])){
					foreach($item['aggregation'] as $params){
						try{
							$command->run($processSequence, $params);
						}catch(Exception $exception){
							return $processSequence;
						}
					}
				}else{
					try{
						$command->run($processSequence, $item['params']);
					}catch(Exception $exception){
						return $processSequence;
					}
				}
			}
			return $processSequence;
		}

	}
}
{'fraction_non_alphanumeric': 0.15534382767191385, 'fraction_numerical': 0.006628003314001657, 'mean_word_length': 3.447513812154696, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 20, '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': '6804665', 'n_tokens_mistral': 918, 'n_tokens_neox': 727, 'n_words': 147}