<?php

/**
 * Class file for decoding API key audit logs.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package clearFusionCMS
 * @copyright Copyright (c) 2016 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/**
 * cfApiKeyAuditHelper Class
 *
 * Class to provide decoding for API key audit trails.
 *
 * @package clearFusionCMS
 */
class cfApiKeyAuditHelper extends cfAuditHelper {

	/**
	 * Convert the raw data to a human readable message.
	 *
	 * @param array $data The audit data.
	 * @return string The message string.
	 */
	function getMessage($data) {
		$params = empty($data['params']) ? [] : json_decode($data['params'], true);

		$roleList = '';
		if(isset($params['roles']) && count($params['roles'])) {
			$roleList = '<strong>' . implode('</strong>, <strong>', $params['roles']) . '</strong>';
		}

		switch($data['action']) {
			case cfApiKeyAudit::NEWKEY:
				$msg = sprintf(__('Created a new API key <strong>%1s</strong> with roles: %2s.', 'cfBackend'), $params['name'], $roleList);
				break;
			case cfApiKeyAudit::UPDATEKEY:
				$msg = sprintf(__('Updated the API key <strong>%1s</strong> with roles: %2s.', 'cfBackend'), $params['name'], $roleList);
				break;
			case cfApiKeyAudit::DELKEY:
				$msg = sprintf(__('Deleted the API key <strong>%s</strong>.', 'cfBackend'), $params['name']);
				break;
			default:
				$msg = parent::getMessage($data);
				break;
		}

		return $msg;
	}

	/**
	 * @inheritdoc
	 */
	function hasAdditionalData($data) {
		return in_array($data['action'], [ cfApiKeyAudit::UPDATEKEY, cfApiKeyAudit::DELKEY ]);
	}
}
