<commit_before><?php
/**
 * Provision Engine to provision new Akamai HLS+HDS live stream	
 * 
 * @package Scheduler
 * @subpackage Provision
 */
class KProvisionEngineUniversalAkamai extends KProvisionEngine
{
	/**
	 * @var AkamaiUniversalStreamClient
	 */
	protected $streamClient;
	
	protected function __construct($taskConfig, KalturaAkamaiUniversalProvisionJobData $data)
	{
		if (!$this->taskConfig->params->restapi->akamaiRestApiBaseServiceUrl)
			return new KProvisionEngineResult(KalturaBatchJobStatus::FAILED, "Error: akamaiRestApiBaseServiceUrl is missing from worker configuration. Cannot provision stream"); 
		
		AkamaiUniversalStreamClient::$baseServiceUrl = $this->taskConfig->params->akamaiRestApiBaseServiceUrl;
		parent::__construct($taskConfig);
		
		$username = null;
		$password = <PASSWORD>;
		
		if (!is_null($data) && $data instanceof KalturaAkamaiUniversalProvisionJobData)
		{
			//all fields are set and are not empty string
			if ($data->systemUserName && $data->systemPassword && $data->domainName)
			{
				$username = $data->systemUserName;
				$password = $<PASSWORD>;
				$domainName = $data->domainName;
			}
		}
		//if one of the params was not set, use the taskConfig data	
		if (!$username || !$password )
		{
			$username = $this->taskConfig->params->restapi->systemUserName;
			$password = $this->taskConfig->params->restapi->systemPassword;
			$domainName = $this->taskConfig->params->restapi->domainName;
			$data->primaryContact = $this->taskConfig->restapi->primaryContact;
			$data->secondaryContact = $this->taskConfig->restapi->secondaryContact;
			$data->notificationEmail = $this->taskConfig->restapi->notificationEmail;
		}
		
		KalturaLog::debug("Connecting to Akamai(username: $username, password: $password, domain: $domainName)");
		$this->streamClient = new AkamaiUniversalStreamClient($username, $password, $domainName);
	}
	
	/* (non-PHPdoc)
	 * @see KProvisionEngine::getName()
	 */
	public function getName() {
		return get_class($this);
		
	}

	/* (non-PHPdoc)
	 * @see KProvisionEngine::provide()
	 */
	public function provide(KalturaBatchJob $job, KalturaProvisionJobData $data) 
	{
		/* @var $data KalturaAkamaiUniversalProvisionJobData */
		$streamConfiguration = new KalturaAkamaiUniversalStreamConfiguration();
		//Construct stream configuration
		$streamConfiguration->streamName = $data->streamName;
		$streamConfiguration->streamType = $data->streamType;
		
		$res = $this->streamClient->provisionStream($streamConfiguration);
		if (!$res)
		{
			return new KProvisionEngineResult(KalturaBatchJobStatus::FAILED, "Error: no result received for connection"); 
		}
		
		KalturaLog::info ("Request to provision stream returned result: $res");
		$resultXML = new SimpleXMLElement($res);
		//In this case, REST API has returned an API error.
		$errors = $resultXML->xpath('error');
		if ($errors && count($errors))
		{
			//There is always only 1 error listed in the XML
			$error = $errors[0];
			return new KProvisionEngineResult(KalturaBatchJobStatus::FAILED, "Error: ". strval($error[0]));
		}
		//Otherwise, the stream provision request probably returned OK, attempt to parse it as a new stream XML
		try {<commit_msg>fwr: set stream configuration before sending email<commit_after><?php
/**
 * Provision Engine to provision new Akamai HLS+HDS live stream	
 * 
 * @package Scheduler
 * @subpackage Provision
 */
class KProvisionEngineUniversalAkamai extends KProvisionEngine
{
	/**
	 * @var AkamaiUniversalStreamClient
	 */
	protected $streamClient;
	
	protected function __construct($taskConfig, KalturaAkamaiUniversalProvisionJobData $data)
	{
		if (!$this->taskConfig->params->restapi->akamaiRestApiBaseServiceUrl)
			return new KProvisionEngineResult(KalturaBatchJobStatus::FAILED, "Error: akamaiRestApiBaseServiceUrl is missing from worker configuration. Cannot provision stream"); 
		
		AkamaiUniversalStreamClient::$baseServiceUrl = $this->taskConfig->params->akamaiRestApiBaseServiceUrl;
		parent::__construct($taskConfig);
		
		$username = null;
		$password = null;
		
		if (!is_null($data) && $data instanceof KalturaAkamaiUniversalProvisionJobData)
		{
			//all fields are set and are not empty string
			if ($data->systemUserName && $data->systemPassword && $data->domainName)
			{
				$username = $data->systemUserName;
				$password = $data->systemPassword;
				$domainName = $data->domainName;
			}
		}
		//if one of the params was not set, use the taskConfig data	
		if (!$username || !$password )
		{
			$username = $this->taskConfig->params->restapi->systemUserName;
			$password = $this->taskConfig->params->restapi->systemPassword;
			$domainName = $this->taskConfig->params->restapi->domainName;
			$data->primaryContact = $this->taskConfig->restapi->primaryContact;
			$data->secondaryContact = $this->taskConfig->restapi->secondaryContact;
			$data->notificationEmail = $this->taskConfig->restapi->notificationEmail;
		}
		
		KalturaLog::debug("Connecting to Akamai(username: $username, password: <PASSWORD>, domain: $domainName)");
		$this->streamClient = new AkamaiUniversalStreamClient($username, $password, $domainName);
	}
	
	/* (non-PHPdoc)
	 * @see KProvisionEngine::getName()
	 */
	public function getName() {
		return get_class($this);
		
	}

	/* (non-PHPdoc)
	 * @see KProvisionEngine::provide()
	 */
	public function provide(KalturaBatchJob $job, KalturaProvisionJobData $data) 
	{
		/* @var $data KalturaAkamaiUniversalProvisionJobData */
		$streamConfiguration = new KalturaAkamaiUniversalStreamConfiguration();
		//Construct stream configuration
		$streamConfiguration->streamName = $data->streamName;
		$streamConfiguration->streamType = $data->streamType;
		$streamConfiguration->primaryContact = $data->primaryContact;
		$streamConfiguration->secondaryContact = $data->secondaryContact;
		$streamConfiguration->dvrEnabled = $data->dvrEnabled;
		$streamConfiguration->dvrWindow = $data->dvrWindow;
		$streamConfiguration->encoderPassword = $data->encoderPassword;
		$streamConfiguration->primaryEncodingIP = $data->encoderIP;
		$streamConfiguration->secondaryEncodingIP = $data->backupEncoderIP;
		$streamConfiguration->notificationEmail = $data->notificationEmail;
		
		$res = $this->streamClient->provisionStream($streamConfiguration);
		if (!$res)
		{
			return new KProvisionEngineResult(KalturaBatchJobStatus::FAILED, "Error: no result received for connection"); 
		}
		
		KalturaLog::info ("Request to provision stream returned result: $res");
		$resultXML = new SimpleXMLElement($res);
		//In this case, REST API has returned an API error.
		$errors = $resultXML->xpath('error');
		if ($errors && count($errors))
		{
			//There is always only 1 error listed in the XML
			$error = $errors[0];
			return new KProvisionEngineResult(KalturaBatchJobStatus::FAILED, "Error: ". strval($error[0]));
		}
		//Otherwise, the stream provision request probably returned OK, attempt to parse it as a new stream XML
		try {
{'fraction_non_alphanumeric': 0.1265530193585669, 'fraction_numerical': 0.0008668015024559376, 'mean_word_length': 5.148312611012433, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 111, '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': '6000457', 'n_tokens_mistral': 2256, 'n_tokens_neox': 2014, 'n_words': 458}