oai-pmh2/src/Middleware.php

179 lines
5.5 KiB
PHP
Raw Normal View History

2024-01-03 16:54:13 +01:00
<?php
/**
* OAI-PMH 2.0 Data Provider
2024-01-22 15:06:07 +01:00
* Copyright (C) 2024 Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
2024-01-03 16:54:13 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2024-01-22 15:06:07 +01:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2024-01-03 16:54:13 +01:00
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2024-01-22 15:06:07 +01:00
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2024-01-03 16:54:13 +01:00
*/
declare(strict_types=1);
namespace OCC\OaiPmh2;
2024-09-30 19:37:29 +02:00
use DOMElement;
2024-01-03 16:54:13 +01:00
use GuzzleHttp\Psr7\Utils;
2024-09-30 19:37:29 +02:00
use OCC\OaiPmh2\Entity\Token;
2024-01-03 16:54:13 +01:00
use OCC\OaiPmh2\Middleware\ErrorHandler;
use OCC\PSR15\AbstractMiddleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
/**
* Base class for all OAI-PMH requests.
*
* @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
2024-07-13 18:49:54 +02:00
* @package OAIPMH2
2024-09-30 19:37:29 +02:00
*
* @psalm-type OaiRequestMetadata = array{
* verb: string,
* identifier: ?string,
* metadataPrefix: ?string,
* from: ?string,
* until: ?string,
* set: ?string,
* resumptionToken: ?string,
* counter: int,
* completeListSize: int
* }
2024-01-03 16:54:13 +01:00
*/
abstract class Middleware extends AbstractMiddleware
{
2024-09-30 19:37:29 +02:00
/**
* This holds the request metadata.
*
* @var OaiRequestMetadata
*/
protected array $arguments = [
'verb' => '',
'identifier' => null,
'metadataPrefix' => null,
'from' => null,
'until' => null,
'set' => null,
'resumptionToken' => null,
'counter' => 0,
'completeListSize' => 0
];
/**
* This holds the entity manager singleton.
*/
protected EntityManager $em;
2024-01-03 16:54:13 +01:00
/**
* This holds the prepared response document.
*/
2024-09-30 19:37:29 +02:00
protected Response $preparedResponse;
/**
* Add resumption token information to response document.
*
* @param DOMElement $node The DOM node to add the resumption token to
* @param ?Token $token The new resumption token or NULL if none
*
* @return void
*/
protected function addResumptionToken(DOMElement $node, ?Token $token): void
{
if (isset($token) || isset($this->arguments['resumptionToken'])) {
2024-10-14 14:51:42 +02:00
$resumptionToken = $this->preparedResponse->createElement('resumptionToken');
2024-09-30 19:37:29 +02:00
if (isset($token)) {
$resumptionToken->nodeValue = $token->getToken();
$resumptionToken->setAttribute(
2024-10-14 14:51:42 +02:00
'expirationDate',
$token->getValidUntil()->format('Y-m-d\TH:i:s\Z')
2024-09-30 19:37:29 +02:00
);
$this->arguments['completeListSize'] = $token->getParameters()['completeListSize'];
}
$resumptionToken->setAttribute(
2024-10-14 14:51:42 +02:00
'completeListSize',
(string) $this->arguments['completeListSize']
2024-09-30 19:37:29 +02:00
);
$resumptionToken->setAttribute(
2024-10-14 14:51:42 +02:00
'cursor',
(string) ($this->arguments['counter'] * Configuration::getInstance()->maxRecords)
2024-09-30 19:37:29 +02:00
);
2024-10-14 14:51:42 +02:00
$node->appendChild($resumptionToken);
2024-09-30 19:37:29 +02:00
}
}
/**
* Check for resumption token and populate request arguments.
*
* @return void
*/
protected function checkResumptionToken(): void
{
if (isset($this->arguments['resumptionToken'])) {
2024-10-14 14:51:42 +02:00
$token = $this->em->getResumptionToken($this->arguments['resumptionToken'], $this->arguments['verb']);
2024-09-30 19:37:29 +02:00
if (isset($token)) {
$this->arguments = array_merge($this->arguments, $token->getParameters());
} else {
2024-10-14 14:51:42 +02:00
ErrorHandler::getInstance()->withError('badResumptionToken');
2024-09-30 19:37:29 +02:00
}
}
}
2024-01-03 16:54:13 +01:00
/**
* Prepare response document.
*
* @param ServerRequestInterface $request The pre-processed request
*
* @return void
*/
abstract protected function prepareResponse(ServerRequestInterface $request): void;
/**
* Process an incoming server request.
*
* @param ServerRequestInterface $request The incoming server request
*
* @return ServerRequestInterface The processed server request
*/
protected function processRequest(ServerRequestInterface $request): ServerRequestInterface
{
2024-09-30 19:37:29 +02:00
/** @var OaiRequestMetadata */
$arguments = $request->getAttributes();
$this->arguments = array_merge($this->arguments, $arguments);
2024-10-14 14:51:42 +02:00
$this->prepareResponse($request);
2024-01-03 16:54:13 +01:00
return $request;
}
/**
* Process an incoming response before.
*
* @param ResponseInterface $response The incoming response
*
* @return ResponseInterface The processed response
*/
protected function processResponse(ResponseInterface $response): ResponseInterface
{
if (!ErrorHandler::getInstance()->hasErrors() && isset($this->preparedResponse)) {
2024-10-14 14:51:42 +02:00
$response = $response->withBody(Utils::streamFor((string) $this->preparedResponse));
2024-01-03 16:54:13 +01:00
}
return $response;
}
2024-07-18 14:10:36 +02:00
2024-07-20 12:00:15 +02:00
/**
* The constructor must have the same signature for all derived classes, thus make it final.
2024-09-30 19:37:29 +02:00
*
* @see https://psalm.dev/229
2024-07-20 12:00:15 +02:00
*/
2024-07-18 14:10:36 +02:00
final public function __construct()
{
2024-09-30 19:37:29 +02:00
$this->em = EntityManager::getInstance();
2024-07-18 14:10:36 +02:00
}
2024-01-03 16:54:13 +01:00
}