<?php
/**
 * Detector orchestration.
 *
 * @package LocalGuardScanner
 */

namespace LocalGuardScanner\Detection;

use LocalGuardScanner\Domain\Detection\Detector;
use LocalGuardScanner\Domain\Detection\Evidence;
use LocalGuardScanner\Domain\Detection\MalwareHeuristicDetector;
use LocalGuardScanner\Domain\Detection\ScannableObject;
use LocalGuardScanner\Detection\Integrity\FileTrustEvaluator;

/**
 * Runs independent detectors and merges their evidence.
 */
final class DetectorEngine {
	/** @var Detector[] */
	private $detectors;

	/** @param Detector[] $detectors Registered detectors. */
	public function __construct( array $detectors ) {
		$this->detectors = $detectors;
	}

	/**
	 * @param ScannableObject $scanned_object Object to inspect.
	 * @return Evidence[]
	 */
	public function inspect( ScannableObject $scanned_object ) {
		$evidence   = array();
		$attributes = $scanned_object->attributes();
		$is_trusted = isset( $attributes['trust'] ) && FileTrustEvaluator::TRUSTED_CORE === $attributes['trust'];
		foreach ( $this->detectors as $detector ) {
			if ( $is_trusted && $detector instanceof MalwareHeuristicDetector ) {
				continue;
			}
			foreach ( $detector->inspect( $scanned_object ) as $observation ) {
				$evidence[] = $observation;
			}
		}
		return $evidence;
	}
}
