diff --git "a/\\" "b/\\" new file mode 100644 index 00000000000..5fbbb1a17a5 --- /dev/null +++ "b/\\" @@ -0,0 +1,166 @@ + + * @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 + * @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())); + } +}