Skip to content

Commit

Permalink
[TASK] Support new Avalex API 3.0 with multilanguage texts
Browse files Browse the repository at this point in the history
  • Loading branch information
pascal20997 committed Feb 10, 2022
1 parent 15935e9 commit 4d30177
Show file tree
Hide file tree
Showing 9 changed files with 342 additions and 85 deletions.
21 changes: 17 additions & 4 deletions Classes/AvalexPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,26 @@ public function render($_, $conf)
{
$endpoint = $this->checkEndpoint($conf['endpoint']);
$rootPage = tx_avalex_AvalexUtility::getRootForPage();
$cacheIdentifier = sprintf('avalex_%s_%d_%d', $endpoint, $rootPage, $GLOBALS['TSFE']->id);
$cacheIdentifier = sprintf(
'avalex_%s_%d_%d_%s',
$endpoint,
$rootPage,
$GLOBALS['TSFE']->id,
tx_avalex_AvalexUtility::getFrontendLocale()
);
if ($this->cache->has($cacheIdentifier)) {
$content = (string)$this->cache->get($cacheIdentifier);
} else {
/** @var tx_avalex_AvalexConfigurationRepository $avalexConfigurationRepository */
$avalexConfigurationRepository = t3lib_div::makeInstance(
'tx_avalex_AvalexConfigurationRepository'
);
$configuration = $avalexConfigurationRepository->findByWebsiteRoot($rootPage, 'uid, api_key, domain');
$language = t3lib_div::makeInstance('tx_avalex_LanguageService', $configuration)->getLanguageForEndpoint($endpoint);
/** @var tx_avalex_ApiService $apiService */
$apiService = t3lib_div::makeInstance('tx_avalex_ApiService');
$content = $apiService->getHtmlForCurrentRootPage($endpoint, $rootPage);
$curlInfo = $apiService->getCurlInfo();
$content = $apiService->getHtmlForCurrentRootPage($endpoint, $language, $configuration);
$curlInfo = $apiService->getCurlService()->getCurlInfo();
if ($curlInfo['http_code'] === 200) {
// set cache for successful calls only
$configuration = tx_avalex_AvalexUtility::getExtensionConfiguration();
Expand All @@ -86,7 +98,8 @@ public function render($_, $conf)
$cacheIdentifier,
$content,
array(),
$configuration['cacheLifetime'] ? $configuration['cacheLifetime'] : 3600);
$configuration['cacheLifetime'] ? $configuration['cacheLifetime'] : 3600
);
}
}
return $content;
Expand Down
100 changes: 31 additions & 69 deletions Classes/Service/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@
class tx_avalex_ApiService
{
/**
* @var array
*/
protected $curlInfo = array();

/**
* @var string
* @var tx_avalex_CurlService
*/
protected $curlOutput = '';
protected $curlService;

/**
* @var array
Expand All @@ -40,47 +35,22 @@ public function __construct()
$this->hookObjectsArray[$key] = $hookObject;
}
}
}

/**
* Checks the JSON response
*
* @return bool Returns true if given data is valid or false in case of an error
*/
public function checkResponse()
{
$success = true;
if ($this->curlInfo['http_code'] !== 200) {
$success = false;
t3lib_div::sysLog(
sprintf(
'The avalex API answered with code "%d" and message: "%s".',
$this->curlInfo['http_code'],
$this->curlOutput
),
t3lib_div::SYSLOG_SEVERITY_ERROR
);
}
return $success;
$this->curlService = t3lib_div::makeInstance('tx_avalex_CurlService');
}

/**
* Get HTML content for current page
*
* @param string $endpoint API endpoint to be used e.g. imprint
* @param int $rootPage
* @param string $endpoint API endpoint to be used e.g. imprint
* @param string $language two digit iso code (en, de, ...)
* @param array $configuration required values: api_key: '', domain: ''
*
* @return string
*/
public function getHtmlForCurrentRootPage($endpoint, $rootPage)
public function getHtmlForCurrentRootPage($endpoint, $language, array $configuration)
{
$endpoint = (string)$endpoint;
$rootPage = (int)$rootPage;

/** @var tx_avalex_AvalexConfigurationRepository $avalexConfigurationRepository */
$avalexConfigurationRepository = t3lib_div::makeInstance(
'tx_avalex_AvalexConfigurationRepository'
);
$configuration = $avalexConfigurationRepository->findByWebsiteRoot($rootPage, 'uid, api_key, domain');
$language = (string)$language;

// Hook: Allow to modify $apiKey and $domain before curl sends the request to avalex
foreach ($this->hookObjectsArray as $hookObject) {
Expand All @@ -89,32 +59,32 @@ public function getHtmlForCurrentRootPage($endpoint, $rootPage)
}
}

$curlResource = curl_init();

curl_setopt_array($curlResource, array(
CURLOPT_URL => sprintf(
'%s%s?apikey=%s&domain=%s',
tx_avalex_AvalexUtility::getApiUrl(),
$endpoint,
$configuration['api_key'],
$configuration['domain']
),
CURLOPT_RETURNTRANSFER => true,
$requestSuccessful = $this->curlService->request(sprintf(
'%s%s?apikey=%s&domain=%s&lang=%s',
tx_avalex_AvalexUtility::getApiUrl(),
$endpoint,
$configuration['api_key'],
$configuration['domain'],
$language
));

$this->curlOutput = (string)curl_exec($curlResource);
$this->curlInfo = curl_getinfo($curlResource);

curl_close($curlResource);
$curlInfo = $this->curlService->getCurlInfo();

if ($this->checkResponse()) {
$content = $this->curlOutput;
if ($requestSuccessful === false) {
// curl error
$content = sprintf(
$GLOBALS['LANG']->sL('LLL:EXT:avalex/Resources/Private/Language/locallang.xml:error.request_failed'),
$this->curlService->getCurlErrno(),
$this->curlService->getCurlError()
);
} elseif ((int)$curlInfo['http_code'] === 200) {
$content = $this->curlService->getCurlOutput();
} else {
// render error message wrapped with translated notice in frontend if request !== 200
$content = sprintf(
$GLOBALS['LANG']->sL('LLL:EXT:avalex/Resources/Private/Language/locallang.xml:error.request_failed'),
(int)$this->curlInfo['http_code'],
$this->curlOutput
(int)$curlInfo['http_code'],
$this->curlService->getCurlOutput()
);
}

Expand All @@ -129,18 +99,10 @@ public function getHtmlForCurrentRootPage($endpoint, $rootPage)
}

/**
* @return array
*/
public function getCurlInfo()
{
return $this->curlInfo;
}

/**
* @return string
* @return tx_avalex_CurlService
*/
public function getCurlOutput()
public function getCurlService()
{
return $this->curlOutput;
return $this->curlService;
}
}
114 changes: 114 additions & 0 deletions Classes/Service/CurlService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
/*
* This file is part of the avalex project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

/**
* Service to execute remote requests using curl
*
* Request page:
* $this->request('https://domain.tld/api-request');
* Get http status code:
* $this->getCurlInfo()['http_code'];
* Get response content:
* $this->getCurlOutput();
*/
class tx_avalex_CurlService
{
/**
* @var array
*/
protected $curlInfo = array();

/**
* @var string
*/
protected $curlOutput = '';

/**
* @var string
*/
protected $curlError = '';

/**
* @var int
*/
protected $curlErrno = 0;

/**
* @param $url
* @return bool true on success otherwise false
*/
public function request($url)
{
$curlResource = curl_init();

curl_setopt_array($curlResource, array(
CURLOPT_URL => (string)$url,
CURLOPT_RETURNTRANSFER => true,
));

$this->curlOutput = (string)curl_exec($curlResource);
$this->curlInfo = curl_getinfo($curlResource);
$this->curlError = curl_error($curlResource);
$this->curlErrno = curl_errno($curlResource);

curl_close($curlResource);

if ($this->curlError) {
t3lib_div::sysLog(
sprintf(
'tx_avalex_CurlService::request with URL "%s" failed! Curl error (%d): "%s"',
$url,
$this->curlErrno,
$this->curlError
),
'avalex_legacy',
t3lib_div::SYSLOG_SEVERITY_ERROR
);
return false;
}
return true;
}

/**
* @return array
*/
public function getCurlInfo()
{
return $this->curlInfo;
}

/**
* @return string
*/
public function getCurlOutput()
{
return $this->curlOutput;
}

/**
* @return string
*/
public function getCurlError()
{
return $this->curlError;
}

/**
* @return int
*/
public function getCurlErrno()
{
return $this->curlErrno;
}
}
Loading

0 comments on commit 4d30177

Please sign in to comment.