<commit_before><?php
/**
 * @package plugins.tvinciDistribution
 * @subpackage lib
 */
class TvinciDistributionFeedEngine extends DistributionEngine implements
	IDistributionEngineUpdate,
	IDistributionEngineSubmit,
	IDistributionEngineReport,
	IDistributionEngineDelete
{

	const SOAP_ENVELOPE_URL = 'http://www.w3.org/2003/05/soap-envelope';

	/* (non-PHPdoc)
	 * @see IDistributionEngineSubmit::submit()
	 */
	public function submit(KalturaDistributionSubmitJobData $data)
	{
		$this->validateProviderDataAndDistributionProfile($data);
		$this->handleAction($data, $data->distributionProfile, $data->providerData, "Submit");
		return true;
	}

	/* (non-PHPdoc)
	 * @see IDistributionEngineUpdate::update()
	 */
	public function update(KalturaDistributionUpdateJobData $data)
	{
		$this->validateProviderDataAndDistributionProfile($data);
		$this->handleAction($data, $data->distributionProfile, $data->providerData, "Update");
		return true;
	}

	/* (non-PHPdoc)
	 * @see IDistributionEngineDelete::delete()
	*/
	public function delete(KalturaDistributionDeleteJobData $data)
	{
		$this->validateProviderDataAndDistributionProfile($data);
		$this->handleAction($data, $data->distributionProfile, $data->providerData, "Delete");
		return true;
	}

	private function validateProviderDataAndDistributionProfile(KalturaDistributionJobData $data){
		if (!$data->distributionProfile || !($data->distributionProfile instanceof KalturaTvinciDistributionProfile))
			throw new Exception("Distribution profile must be of type KalturaTvinciDistributionProfile");
		if (!$data->providerData || !($data->providerData instanceof KalturaTvinciDistributionJobProviderData))
			throw new Exception("Provider data must be of type KalturaTvinciDistributionJobProviderData");
	}


	/* (non-PHPdoc)
	 * @see IDistributionEngineReport::fetchReport()
	 */
	public function fetchReport(KalturaDistributionFetchReportJobData $data)
	{
		return false;
	}

	/**
	 * @param KalturaDistributionJobData $data
	 * @param KalturaTvinciDistributionProfile $distributionProfile
	 * @param KalturaTvinciDistributionJobProviderData $providerData
	 * @param $actionType
	 */
	private function handleAction(KalturaDistributionJobData $data, KalturaTvinciDistributionProfile $distributionProfile,
								  KalturaTvinciDistributionJobProviderData $providerData, $actionType){
		$url = $distributionProfile->ingestUrl;
		KalturaLog::info("Tvinci Distribution action {$actionType}".
						 ",entry {$data->entryDistribution->entryId}, url: {$url}\nXML data:\n{$providerData->xml}");

		$result = $this->postXml($url, $providerData->xml);
		$success = ($result->status == 'OK' && $result->tvmID != '');
		if (!$success) {
			KalturaLog::err("Tvinci distribution action {$actionType} has failed with description: {$result->description} ".
							"and status: {$result->status}");
			throw new KalturaDistributionException("{$actionType} failed - reason {$result->description}");
		}
	}

	/**
	 * @param string $url
	 * @param string $xml
	 * @throws Exception in case of failure to receive a response
	 * @return SimpleXMLElement
	 */
	protected function postXml($url, $xml)
	{
		$response = self::curlPost($url, $xml);
		KalturaLog::info("Post XML url:{$url} , XML:{$xml}, Full response: " . print_r($response,true));

		$retrunObject = null;
		if ( $response['http_code'] == KCurlHeaderResponse::HTTP_STATUS_OK )
		{
			try
			{
				/**
				 * this is an exemplary response from the OTT servers per legal request
				 * <s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
				 * 	<s:Header>
				 * 		<a:Action s:mustUnderstand="1">urn:Iservice/InjestTvinciDataResponse</a:Action>
				 * </s:Header>
				 * <s:Body>
				 * 		<InjestTvinciDataResponse>
				 * 			<InjestTvinciDataResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
				 * 				<status>OK</status>
				 * 				<description/>
				 * 				<assetID>1234Wicked</assetID><commit_msg>MediaPrep MediaPrep<commit_after><?php
/**
 * @package plugins.tvinciDistribution
 * @subpackage lib
 */
class TvinciDistributionFeedEngine extends DistributionEngine implements
	IDistributionEngineUpdate,
	IDistributionEngineSubmit,
	IDistributionEngineReport,
	IDistributionEngineDelete
{

	const SOAP_ENVELOPE_URL = 'http://www.w3.org/2003/05/soap-envelope';

	/* (non-PHPdoc)
	 * @see IDistributionEngineSubmit::submit()
	 */
	public function submit(KalturaDistributionSubmitJobData $data)
	{
		return $this->handleAction($data, $data->distributionProfile, $data->providerData, "Submit");
	}

	/* (non-PHPdoc)
	 * @see IDistributionEngineUpdate::update()
	 */
	public function update(KalturaDistributionUpdateJobData $data)
	{
		return $this->handleAction($data, $data->distributionProfile, $data->providerData, "Update");
	}

	/* (non-PHPdoc)
	 * @see IDistributionEngineDelete::delete()
	*/
	public function delete(KalturaDistributionDeleteJobData $data)
	{
		return $this->handleAction($data, $data->distributionProfile, $data->providerData, "Delete");
	}

	private function validateProviderDataAndDistributionProfile(KalturaDistributionJobData $data){
		if (!$data->distributionProfile || !($data->distributionProfile instanceof KalturaTvinciDistributionProfile))
			throw new Exception("Distribution profile must be of type KalturaTvinciDistributionProfile");
		if (!$data->providerData || !($data->providerData instanceof KalturaTvinciDistributionJobProviderData))
			throw new Exception("Provider data must be of type KalturaTvinciDistributionJobProviderData");
	}


	/* (non-PHPdoc)
	 * @see IDistributionEngineReport::fetchReport()
	 */
	public function fetchReport(KalturaDistributionFetchReportJobData $data)
	{
		return false;
	}

	/**
	 * @param KalturaDistributionJobData $data
	 * @param KalturaTvinciDistributionProfile $distributionProfile
	 * @param KalturaTvinciDistributionJobProviderData $providerData
	 * @param $actionType
	 */
	private function handleAction(KalturaDistributionJobData $data, KalturaTvinciDistributionProfile $distributionProfile,
								  KalturaTvinciDistributionJobProviderData $providerData, $actionType){
		$this->validateProviderDataAndDistributionProfile($data);
		$url = $distributionProfile->ingestUrl;
		KalturaLog::info("Tvinci Distribution action {$actionType}".
						 ",entry {$data->entryDistribution->entryId}, url: {$url}\nXML data:\n{$providerData->xml}");

		$result = $this->postXml($url, $providerData->xml);
		$success = ($result->status == 'OK' && $result->tvmID != '');
		if (!$success) {
			KalturaLog::err("Tvinci distribution action {$actionType} has failed with description: {$result->description} ".
							"and status: {$result->status}");
			throw new KalturaDistributionException("{$actionType} failed - reason {$result->description}");
		}
		return true;
	}

	/**
	 * @param string $url
	 * @param string $xml
	 * @throws Exception in case of failure to receive a response
	 * @return SimpleXMLElement
	 */
	protected function postXml($url, $xml)
	{
		$response = self::curlPost($url, $xml);
		KalturaLog::info("Post XML Full response: " . print_r($response,true));

		$retrunObject = null;
		if ( $response['http_code'] == KCurlHeaderResponse::HTTP_STATUS_OK )
		{
			try
			{
				/**
				 * this is an exemplary response from the OTT servers per legal request
				 * <s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
				 * 	<s:Header>
				 * 		<a:Action s:mustUnderstand="1">urn:Iservice/InjestTvinciDataResponse</a:Action>
				 * </s:Header>
				 * <s:Body>
				 * 		<InjestTvinciDataResponse>
				 * 			<InjestTvinciDataResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
				 * 				<status>OK</status>
				 * 				<description/>
				 * 				<assetID>1234Wicked</assetID>
{'fraction_non_alphanumeric': 0.13456599897277863, 'fraction_numerical': 0.007960965588084233, 'mean_word_length': 5.241185897435898, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 17}, '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': '986300', 'n_tokens_mistral': 2697, 'n_tokens_neox': 2354, 'n_words': 439}