Chamilo is a learning management system focused on ease of use and accessibility
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
chamilo-lms/plugin/whispeakauth/Controller/BaseRequestController.php

142 lines
3.4 KiB

<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\WhispeakAuth\Controller;
use FFMpeg\FFMpeg;
use FFMpeg\Format\Audio\Wav;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\RequestException;
/**
* Class BaseRequestController.
*
* @package Chamilo\PluginBundle\WhispeakAuth\Controller
*/
abstract class BaseRequestController
{
/**
* @var \WhispeakAuthPlugin
*/
protected $plugin;
/**
* @var \GuzzleHttp\Client
*/
protected $httpClient;
/**
* @var string
*/
protected $apiKey;
/**
* @var \Chamilo\UserBundle\Entity\User
*/
protected $user;
/**
* @var string
*/
protected $audioFilePath;
/**
* BaseController constructor.
*/
public function __construct()
{
$this->plugin = \WhispeakAuthPlugin::create();
$this->httpClient = new Client(['base_uri' => $this->plugin->getApiUrl(),]);
$this->apiKey = $this->plugin->get(\WhispeakAuthPlugin::SETTING_TOKEN);
}
abstract protected function setUser();
/**
* @return bool
*/
abstract protected function userIsAllowed();
/**
* @throws \Exception
*/
protected function protect()
{
if (false === $this->userIsAllowed()) {
throw new \Exception(get_lang('NotAllowed'));
}
$this->plugin->protectTool(false);
}
/**
* @throws \Exception
*/
private function uploadAudioFile()
{
$pluginName = $this->plugin->get_name();
$path = api_upload_file($pluginName, $_FILES['audio'], $this->user->getId());
if (false === $path) {
throw new \Exception(get_lang('UploadError'));
}
$fullPath = api_get_path(SYS_UPLOAD_PATH).$pluginName.$path['path_to_save'];
$mimeType = mime_content_type($fullPath);
if ('wav' !== substr($mimeType, -3)) {
$ffmeg = FFMpeg::create();
$audioFile = $ffmeg->open($fullPath);
$fullPath = dirname($fullPath).'/audio.wav';
$audioFile->save(new Wav(), $fullPath);
}
$this->audioFilePath = $fullPath;
}
public function process()
{
try {
$this->protect();
$this->setUser();
if (empty($this->user)) {
throw new \Exception(get_lang('NoUser'));
}
$this->uploadAudioFile();
$response = $this->doApiRequest();
echo $response;
} catch (\Exception $exception) {
echo \Display::return_message($exception->getMessage(), 'error');
}
}
/**
* @throws \Exception
*
* @return mixed
*/
abstract protected function doApiRequest();
/**
* @param \GuzzleHttp\Exception\RequestException $requestException
* @param string $defaultMessage
*
* @throws \Exception
*/
protected function throwRequestException(RequestException $requestException, $defaultMessage)
{
$message = $defaultMessage;
if ($requestException->hasResponse()) {
$json = json_decode((string) $requestException->getResponse()->getBody(), true);
$message = is_array($json['message']) ? implode("\n", $json['message']) : $json['message'];
}
throw new \Exception($message);
}
}