<?php
namespace App\GdsSalesReport\Parser;

class TraverserFactory
{
	use \App\Utils\BaseParser\SingletonTrait;

	/**
	 * Build a traverser object, configured for the current market
	 * @param string $filepath
	 * @return \App\Bsp\Parser\BspTraverserAbstract
	 */
	public function getParser($filepath, $fileType)
	{
		$fileFormats = array_filter($this->getFileFormats()->getAll(), function($fmt) use($fileType) {
			return $fmt->code == $fileType;
		});
		$fmt = reset($fileFormats);
		$class = $fmt->class;

		$parser = new $class();
		$parser->loadFile($filepath);

		return $parser;
	}

	public function getFileFormats()
	{
		$fileFormats = new \App\GdsSalesReport\Parser\FileFormats\FileFormats();

		// Amadeus
		$fmt = new \App\GdsSalesReport\Parser\FileFormats\FileFormat();
		$fmt->code = 'amadeus';
        $fmt->label = 'Amadeus';
        $fmt->priority = 10;
        $fmt->class = '\App\GdsSalesReport\Parser\Parsers\Amadeus';
		$fileFormats->append($fmt);

		// Sabre
		$fmt = new \App\GdsSalesReport\Parser\FileFormats\FileFormat();
		$fmt->code = 'sabre';
        $fmt->label = 'Sabre';
        $fmt->priority = 10;
        $fmt->class = '\App\GdsSalesReport\Parser\Parsers\Sabre';
		$fileFormats->append($fmt);

		// ARC
		$fmt = new \App\GdsSalesReport\Parser\FileFormats\FileFormat();
		$fmt->code = 'arc';
		$fmt->label = 'ARC';
		$fmt->priority = 10;
		$fmt->class = '\App\GdsSalesReport\Parser\Parsers\ARC';
		$fileFormats->append($fmt);

		\Engine\Plugins\Manager::getInstance()->executeEvent('App\GdsSalesReport\Parser\TraverserFactory-getFileFormats', [
			'fileFormats' => $fileFormats,
		] );
		return $fileFormats;
	}
}
