Skip to content

Commit

Permalink
[API] Fix Travis again
Browse files Browse the repository at this point in the history
  • Loading branch information
spell00 committed Nov 4, 2020
1 parent d3bdd67 commit d36d982
Showing 1 changed file with 166 additions and 0 deletions.
166 changes: 166 additions & 0 deletions \
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php declare(strict_types=1);
/**
* This implements the Project page class under Projects
*
* PHP Version 7
*
* @category API
* @package Loris
* @author Xavier Lecours Boucher <xavier.lecours@mcin.ca>
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://github.com/aces/Loris
*/
namespace LORIS\api\Endpoints\Project;

use \Psr\Http\Message\ServerRequestInterface;
use \Psr\Http\Message\ResponseInterface;
use \LORIS\api\Endpoint;

/**
* A class for handling the /projects/$projectname endpoint.
*
* @category API
* @package Loris
* @author Xavier Lecours Boucher <xavier.lecours@mcin.ca>
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://github.com/aces/Loris
*/
class Project extends Endpoint implements \LORIS\Middleware\ETagCalculator
{
/**
* A cache of the results of the projects/$projectname endpoint, so that
* it doesn't need to be recalculated for the ETag and handler
*/
private $_cache;

/**
* The requested project
*/
private $_project;

/**
* Contructor
*
* @param \Project $project The requested project
*/
public function __construct(\Project $project)
{
$this->_project = $project;
}

/**
* Return which methods are supported by this endpoint.
*
* @return array supported HTTP methods
*/
protected function allowedMethods() : array
{
return ['GET'];
}

/**
* Versions of the LORIS API which are supported by this
* endpoint.
*
* @return array a list of supported API versions.
*/
protected function supportedVersions() : array
{
return [
'v0.0.2',
'v0.0.3',
];
}

/**
* Handles a request that starts with /projects/$projectname
*
* @param ServerRequestInterface $request The incoming PSR7 request
*
* @return ResponseInterface The outgoing PSR7 response
*/
public function handle(ServerRequestInterface $request) : ResponseInterface
{
$pathparts = $request->getAttribute('pathparts');
if (count($pathparts) === 0) {
switch ($request->getMethod()) {
case 'GET':
return $this->_handleGET($request);

case 'OPTIONS':
return (new \LORIS\Http\Response())
->withHeader('Allow', $this->allowedMethods());

default:
return new \LORIS\Http\Response\JSON\MethodNotAllowed(
$this->allowedMethods()
);
}
}

// Delegate to sub-endpoints
$subendpoint = array_shift($pathparts);
switch($subendpoint) {
case 'candidates':
$handler = new Candidates($this->_project);
break;
case 'images':
$handler = new Images($this->_project);
break;
case 'instruments':
$handler = new Instruments($this->_project);
break;
case 'visits':
$handler = new Visits($this->_project);
break;
case 'recordings':
$handler = new Recordings($this->_project);
break;
case 'dicoms':
$handler = new Dicoms($this->_project);
break;
default:
return new \LORIS\Http\Response\JSON\NotFound();
}

$newrequest = $request
->withAttribute('pathparts', $pathparts);

return $handler->process(
$newrequest,
$handler
);
}

/**
* Generates a JSON representation of this project following the API
* specification.
*
* @return ResponseInterface
*/
private function _handleGET(): ResponseInterface
{
if (isset($this->_cache)) {
return $this->_cache;
}

$array = (new \LORIS\api\Views\Project($this->_project))
->toArray();

$this->_cache = new \LORIS\Http\Response\JsonResponse($array);

return $this->_cache;
}

/**
* Implements the ETagCalculator interface
*
* @param ServerRequestInterface $request The PSR7 incoming request.
*
* @return string etag summarizing value of this request.
*/
public function ETag(ServerRequestInterface $request) : string
{
return md5(json_encode($this->_handleGET($request)->getBody()));
}
}

0 comments on commit d36d982

Please sign in to comment.