Skip to content

Commit

Permalink
[TASK] Refactor API eID script
Browse files Browse the repository at this point in the history
  • Loading branch information
dkd-kaehm committed Oct 21, 2022
1 parent 94aa488 commit fcaafc7
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 74 deletions.
138 changes: 116 additions & 22 deletions Classes/Eid/ApiEid.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,130 @@
namespace ApacheSolrForTypo3\Solr\Eid;

use ApacheSolrForTypo3\Solr\Api;
use ApacheSolrForTypo3\Solr\Domain\Site\SiteHashService;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Http\ImmediateResponseException;
use TYPO3\CMS\Core\Http\JsonResponse;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\HttpUtility;

class ApiEid
{
/**
* Globally required params
* @var array
*/
protected const REQUIRED_PARAMS_GLOBAL = [
'api',
'apiKey',
];

/**
* Available methods and params
*
* @var array
*/
protected const API_METHODS = [
'siteHash' => [
'params' => [
'required' => [
'domain',
],
],
],
];

/**
* The main method for eID scripts.
*
* @throws ImmediateResponseException
*/
public function main(ServerRequestInterface $request): ResponseInterface
{
$api = GeneralUtility::_GP('api');
$apiKey = trim(GeneralUtility::_GP('apiKey'));

if (!Api::isValidApiKey($apiKey)) {
header(HttpUtility::HTTP_STATUS_403);
header('Content-Type: application/json; charset=utf-8');
echo json_encode(['errorMessage' => 'Invalid API key']);
} else {
switch ($api) {
case 'siteHash':
include('SiteHash.php');
break;

default:
header(HttpUtility::HTTP_STATUS_400);
header('Content-Type: application/json; charset=utf-8');
echo json_encode(['errorMessage' => 'You must provide an available API method, e.g. siteHash.']);
break;
}
$this->validateRequest($request);
return $this->{'get' . ucfirst($request->getQueryParams()['api']) . 'Response'}($request);
}

/**
* Returns the site hash
*
* @param ServerRequestInterface $request
* @return JsonResponse
* @noinspection PhpUnused
*/
protected function getSiteHashResponse(ServerRequestInterface $request): JsonResponse
{
$domain = $request->getQueryParams()['domain'];

/* @var SiteHashService $siteHashService */
$siteHashService = GeneralUtility::makeInstance(SiteHashService::class);
$siteHash = $siteHashService->getSiteHashForDomain($domain);
return new JsonResponse(
['sitehash' => $siteHash]
);
}

/**
* Validates request.
*
* @throws ImmediateResponseException
*/
protected function validateRequest(ServerRequestInterface $request): void
{
$params = $request->getQueryParams();
if (!Api::isValidApiKey($params['apiKey'])) {
throw new ImmediateResponseException(
new JsonResponse(
['errorMessage' => 'Invalid API key'],
403,
),
403
);
}

if ($params['api'] === null || !array_key_exists($params['api'], self::API_METHODS)) {
throw new ImmediateResponseException(
new JsonResponse(
[
'errorMessage' => 'You must provide an available API method, e.g. siteHash. See: available methods in methods key.',
'methods' => $this->getApiMethodDefinitions(),
],
400
),
400
);
}

$requiredApiParams = $this->getApiMethodDefinitions()[$params['api']]['params']['required'] ?? [];
$requiredApiParams[] = 'eID';
$missingParams = array_values(array_diff($requiredApiParams, array_keys($request->getQueryParams())));
if (!empty($missingParams)) {
throw new ImmediateResponseException(
new JsonResponse(
[
'errorMessage' => 'Required API params are not provided. See: methods.',
'missing_params' => $missingParams,
'methods' => $this->getApiMethodDefinitions()[$params['api']],
],
400
),
400
);
}
}

/**
* Returns the available methods and their params.
*/
protected function getApiMethodDefinitions(): array
{
$apiMethodDefinitions = self::API_METHODS;
foreach ($apiMethodDefinitions as $apiMethodName => $apiMethodDefinition) {
$apiMethodDefinitions[$apiMethodName]['params']['required'] = array_merge(
self::REQUIRED_PARAMS_GLOBAL,
$apiMethodDefinition['params']['required'] ?? []
);
}
return new Response();
return $apiMethodDefinitions;
}
}
52 changes: 0 additions & 52 deletions Classes/Eid/SiteHash.php

This file was deleted.

0 comments on commit fcaafc7

Please sign in to comment.