Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typo3 13 compatibility #46

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions Classes/AvalexPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,27 +217,17 @@ static function ($match) use ($requestUrl, $encryptMailCallable) {
);
}

/**
* @return callable
*/
protected function getEncryptedMailCallable()
protected function getEncryptedMailCallable(): callable
{
$cObj = $this->cObj;

return static function ($mailAddress, $linkText) use ($cObj) {
if (version_compare(Typo3Utility::getTypo3Version(), '12.0', '>=')) {
$linkBuilder = GeneralUtility::makeInstance(EmailLinkBuilder::class, $cObj, $GLOBALS['TSFE']);
return $linkBuilder->processEmailLink((string)$mailAddress, (string)$linkText);
}

return $cObj->getMailTo($mailAddress, $linkText);
$linkBuilder = GeneralUtility::makeInstance(EmailLinkBuilder::class, $cObj, $GLOBALS['TSFE']);
return $linkBuilder->processEmailLink((string)$mailAddress, (string)$linkText);
};
}

/**
* @return LanguageService
*/
protected function getLanguageService(array $configuration)
protected function getLanguageService(array $configuration): LanguageService
{
return GeneralUtility::makeInstance(LanguageService::class, $configuration);
}
Expand Down
35 changes: 14 additions & 21 deletions Classes/Backend/Preview/ContentPreviewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@
use JWeiland\Avalex\Exception\AvalexConfigurationNotFoundException;
use JWeiland\Avalex\Exception\InvalidUidException;
use JWeiland\Avalex\Utility\AvalexUtility;
use TYPO3\CMS\Backend\Preview\StandardContentPreviewRenderer;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\View\BackendLayout\Grid\GridColumnItem;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class ContentPreviewRenderer extends \TYPO3\CMS\Backend\Preview\StandardContentPreviewRenderer
class ContentPreviewRenderer extends StandardContentPreviewRenderer
{
public function __construct(
private readonly AvalexConfigurationRepository $avalexConfigurationRepository,
private readonly UriBuilder $uriBuilder
) {}

public function renderPageModulePreviewContent(GridColumnItem $item): string
{
$itemContent = parent::renderPageModulePreviewContent($item);
Expand All @@ -33,23 +39,20 @@ public function renderPageModulePreviewContent(GridColumnItem $item): string
return $itemContent;
}

$avalexConfigurationRepository = GeneralUtility::makeInstance(
AvalexConfigurationRepository::class
);
$itemContent .= sprintf(
'<p><b>Avalex: %s</b></p>',
$GLOBALS['LANG']->sL(sprintf('LLL:EXT:avalex/Resources/Private/Language/locallang_db.xlf:tx_%s.name', $row['list_type']))
$GLOBALS['LANG']->sL(sprintf('LLL:EXT:avalex/Resources/Private/Language/locallang_db.xlf:tx_%s.name', $row['CType']))
);

try {
$configuration = $avalexConfigurationRepository->findByWebsiteRoot($rootPage, 'uid,description');
$configuration = $this->avalexConfigurationRepository->findByWebsiteRoot($rootPage, 'uid,description');
$itemContent .= sprintf(
'<p>%s</p>',
sprintf($GLOBALS['LANG']->sL('LLL:EXT:avalex/Resources/Private/Language/locallang_db.xlf:preview_renderer.found_config'), $configuration['description'])
);
$itemContent .= sprintf(
'<a href="%s" class="btn btn-default t3-button">%s</a>',
$this->getLinkToEditConfigurationRecord($configuration['uid']),
$this->getLinkToEditConfigurationRecord((int)$configuration['uid']),
$GLOBALS['LANG']->sL('LLL:EXT:avalex/Resources/Private/Language/locallang_db.xlf:preview_renderer.button.edit')
);
} catch (AvalexConfigurationNotFoundException $avalexConfigurationNotFoundException) {
Expand All @@ -67,33 +70,23 @@ public function renderPageModulePreviewContent(GridColumnItem $item): string
return $itemContent;
}

/**
* @param int $uid
*
* @return string
*/
protected function getLinkToEditConfigurationRecord($uid)
protected function getLinkToEditConfigurationRecord(int $uid): string
{
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
$params = [
'edit' => ['tx_avalex_configuration' => [$uid => 'edit']],
'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI'),
];

return (string)$uriBuilder->buildUriFromRoute('record_edit', $params);
return (string)$this->uriBuilder->buildUriFromRoute('record_edit', $params);
}

/**
* @return string
*/
protected function getLinkToCreateConfigurationRecord()
protected function getLinkToCreateConfigurationRecord(): string
{
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
$params = [
'edit' => ['tx_avalex_configuration' => [0 => 'new']],
'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI'),
];

return (string)$uriBuilder->buildUriFromRoute('record_edit', $params);
return (string)$this->uriBuilder->buildUriFromRoute('record_edit', $params);
}
}
44 changes: 14 additions & 30 deletions Classes/Client/AvalexClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use JWeiland\Avalex\Utility\Typo3Utility;
use TYPO3\CMS\Core\Http\RequestFactory;
use TYPO3\CMS\Core\Messaging\AbstractMessage;
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
Expand Down Expand Up @@ -56,7 +57,7 @@ public function processRequest(RequestInterface $request)
$this->messageHelper->addFlashMessage(
$e->getMessage(),
'Request Exception',
AbstractMessage::ERROR
ContextualFeedbackSeverity::ERROR
);
return new AvalexResponse();
}
Expand All @@ -67,53 +68,36 @@ public function processRequest(RequestInterface $request)
return $avalexResponse;
}

/**
* @param RequestInterface $request
* @return AvalexResponse
*/
protected function request(RequestInterface $request)
protected function request(RequestInterface $request): AvalexResponse
{
if (version_compare(Typo3Utility::getTypo3Version(), '8.1', '>=')) {
$requestFactory = GeneralUtility::makeInstance(RequestFactory::class);
$response = $requestFactory->request($request->buildUri());
$content = (string)$response->getBody();
$headers = $response->getHeaders();
$status = $response->getStatusCode();
} else {
$result = [];
$response = GeneralUtility::getUrl($request->buildUri(), 1, null, $result);
list($headers, $content) = explode(CRLF . CRLF, $response);
$status = isset($result['http_code']) ? $result['http_code'] : 0;
}
$requestFactory = GeneralUtility::makeInstance(RequestFactory::class);
$response = $requestFactory->request($request->buildUri());
$content = (string)$response->getBody();
$headers = $response->getHeaders();
$status = $response->getStatusCode();

$avalexResponse = new AvalexResponse($content, $headers, $status);
$avalexResponse->setIsJsonResponse($request->isJsonRequest());

return $avalexResponse;
}

/**
* @return bool
*/
public function hasErrors()
public function hasErrors(): bool
{
return $this->messageHelper->hasErrorMessages();
}

/**
* Check response from Avalex for errors
*
* @param ResponseInterface $response
* @return bool
*/
protected function hasResponseErrors(ResponseInterface $response)
protected function hasResponseErrors(ResponseInterface $response): bool
{
if ($response->isJsonResponse()) {
if (!is_array($response->getBody())) {
$this->messageHelper->addFlashMessage(
'The response of Avalex could not be converted to array.',
'Invalid Avalex JSON response',
AbstractMessage::ERROR
ContextualFeedbackSeverity::ERROR
);
return true;
}
Expand All @@ -122,7 +106,7 @@ protected function hasResponseErrors(ResponseInterface $response)
$this->messageHelper->addFlashMessage(
'The JSON response of Avalex is empty.',
'Empty Avalex JSON response',
AbstractMessage::ERROR
ContextualFeedbackSeverity::ERROR
);
return true;
}
Expand All @@ -131,7 +115,7 @@ protected function hasResponseErrors(ResponseInterface $response)
$this->messageHelper->addFlashMessage(
'The response of Avalex was empty.',
'Empty Avalex response',
AbstractMessage::ERROR
ContextualFeedbackSeverity::ERROR
);
return true;
}
Expand All @@ -140,7 +124,7 @@ protected function hasResponseErrors(ResponseInterface $response)
$this->messageHelper->addFlashMessage(
$response->getBody(),
'Avalex Response Error',
AbstractMessage::ERROR
ContextualFeedbackSeverity::ERROR
);

return true;
Expand Down
86 changes: 19 additions & 67 deletions Classes/Client/Request/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,123 +19,81 @@
*/
abstract class AbstractRequest implements RequestInterface
{
/**
* @var string
*/
private $apiDomain = 'avalex.de';
private string $apiDomain = 'avalex.de';

/**
* @var string
*/
private $apiVersion = '3.0.1';
private string $apiVersion = '3.0.1';

/**
* Is this is set, the required parameter API KEY will be overridden by this value.
* Please use that only within API tests like IsApiKeyConfiguredRequest
*
* @var string
*/
protected $overrideApiKey = '';
protected string $overrideApiKey = '';

/**
* Endpooint is something like "avx-get-domain-langs" or "avx-datenschutzerklaerung"
*
* @link https://documenter.getpostman.com/view/5293147/SWLYDCAk
* @var string
*/
protected $endpoint = '';
protected string $endpoint = '';

/**
* @var bool
*/
protected $isJsonRequest = false;
protected bool $isJsonRequest = false;

/**
* @var array
*/
protected $parameters = [];
protected array $parameters = [];

/**
* @return string
*/
public function getEndpoint()
public function getEndpoint(): string
{
return $this->endpoint;
}

/**
* Endpoint 'avx-datenschutzerklaerung' ==> 'datenschutzerklaerung'
*
* @return string
* Endpoint 'avx-datenschutzerklaerung' - 'datenschutzerklaerung'
*/
public function getEndpointWithoutPrefix()
public function getEndpointWithoutPrefix(): string
{
return substr($this->endpoint, 4);
}

/**
* @return bool
*/
public function isJsonRequest()
public function isJsonRequest(): bool
{
return $this->isJsonRequest;
}

/**
* @return array
*/
public function getParameters()
public function getParameters(): array
{
$this->setRequiredParameters();

return $this->parameters;
}

/**
* @param array $parameters
*/
public function setParameters(array $parameters)
public function setParameters(array $parameters): void
{
$this->parameters = array_intersect_key($parameters, $this->allowedParameters);
}

/**
* @param string $parameter
* @param $value
*/
public function addParameter($parameter, $value)
public function addParameter(string $parameter, mixed $value): void
{
if (array_key_exists($parameter, $this->allowedParameters)) {
$this->parameters[$parameter] = $value;
}
}

/**
* @param string $parameter
* @return mixed
*/
public function getParameter($parameter)
public function getParameter(string $parameter): mixed
{
return $this->parameters[$parameter];
}

/**
* Check, if parameter exists
*
* @param string $parameter
* @return bool
*/
public function hasParameter($parameter)
public function hasParameter(string $parameter): bool
{
return array_key_exists($parameter, $this->parameters);
}

/**
* Merge all parameters to build an URI
*
* @return string
*/
public function buildUri()
public function buildUri(): string
{
return sprintf(
'https://%s/%s?%s',
Expand All @@ -145,10 +103,7 @@ public function buildUri()
);
}

/**
* @return bool
*/
public function isValidRequest()
public function isValidRequest(): bool
{
$isValid = true;
$uri = $this->buildUri();
Expand All @@ -171,7 +126,7 @@ public function isValidRequest()
return $isValid;
}

protected function setRequiredParameters()
protected function setRequiredParameters(): void
{
if ($this instanceof IsApiKeyConfiguredRequest && $this->overrideApiKey !== '') {
$this->addParameter('apikey', $this->overrideApiKey);
Expand Down Expand Up @@ -217,10 +172,7 @@ protected function setRequiredParameters()
}
}

/**
* @return AvalexConfigurationRepository
*/
protected function getAvalexConfigurationRepository()
protected function getAvalexConfigurationRepository(): AvalexConfigurationRepository
{
return GeneralUtility::makeInstance(AvalexConfigurationRepository::class);
}
Expand Down
Loading
Loading