Skip to content

Commit 0539993

Browse files
committed
Change Server class with non-functional code cleanup
- Sort class constants alphabetically - Move getter and setters above constructor - Whitespace fixes
1 parent 356d864 commit 0539993

1 file changed

Lines changed: 72 additions & 55 deletions

File tree

src/Server.php

Lines changed: 72 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ class Server
1111
{
1212
////////////////////////////// CLASS PROPERTIES \\\\\\\\\\\\\\\\\\\\\\\\\\\\
1313

14+
public const ERROR_CAN_NOT_PARSE_FOR_PATCH = 'Could not parse the requested resource for patching';
1415
public const ERROR_CAN_NOT_DELETE_NON_EMPTY_CONTAINER = 'Only empty containers can be deleted, "%s" is not empty';
1516
public const ERROR_NOT_IMPLEMENTED_SPARQL = 'SPARQL Not Implemented';
1617
public const ERROR_PATH_DOES_NOT_EXIST = 'Requested path "%s" does not exist';
1718
public const ERROR_PATH_EXISTS = 'Requested path "%s" already exists';
1819
public const ERROR_POST_EXISTING_RESOURCE = 'Requested path "%s" already exists. Can not "POST" to existing resource. Use "PUT" instead';
19-
public const ERROR_PUT_NON_EXISTING_RESOURCE = self::ERROR_PATH_DOES_NOT_EXIST . '. Can not "PUT" non-existing resource. Use "POST" instead';
2020
public const ERROR_PUT_EXISTING_RESOURCE = self::ERROR_PATH_EXISTS . '. Can not "PUT" existing container.';
21+
public const ERROR_PUT_NON_EXISTING_RESOURCE = self::ERROR_PATH_DOES_NOT_EXIST . '. Can not "PUT" non-existing resource. Use "POST" instead';
2122
public const ERROR_UNKNOWN_HTTP_METHOD = 'Unknown or unsupported HTTP METHOD "%s"';
22-
public const ERROR_CAN_NOT_PARSE_FOR_PATCH = 'Could not parse the requested resource for patching';
23+
2324
private const MIME_TYPE_DIRECTORY = 'directory';
2425
private const QUERY_PARAM_HTTP_METHOD = 'http-method';
2526

@@ -33,51 +34,71 @@ class Server
3334
'POST',
3435
'PUT',
3536
];
37+
/** @var string */
38+
private $baseUrl;
3639
/** @var Filesystem */
3740
private $filesystem;
41+
/** @var string */
42+
private $pubsub;
3843
/** @var Response */
3944
private $response;
40-
private $pubsub;
41-
private $baseUrl;
45+
46+
//////////////////////////// GETTERS AND SETTERS \\\\\\\\\\\\\\\\\\\\\\\\\\\
47+
48+
final public function getFilesystem()
49+
{
50+
return $this->filesystem;
51+
}
52+
53+
final public function getResponse()
54+
{
55+
return $this->response;
56+
}
57+
58+
final public function setBaseUrl($url)
59+
{
60+
$this->baseUrl = $url;
61+
62+
$serverRequest = new \Laminas\Diactoros\ServerRequest(array(),array(), $this->baseUrl);
63+
$this->basePath = $serverRequest->getUri()->getPath();
64+
}
65+
66+
final public function setPubSubUrl($url)
67+
{
68+
$this->pubsub = $url;
69+
}
4270

4371
//////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
4472

4573
final public function __construct(Filesystem $filesystem, Response $response)
4674
{
75+
$this->basePath = '';
76+
$this->baseUrl = '';
77+
$this->pubsub = '';
4778
$this->filesystem = $filesystem;
4879
$this->response = $response;
49-
$this->pubsub = '';
50-
$this->baseUrl = '';
51-
$this->basePath = '';
5280
}
5381

54-
final public function getFilesystem() {
55-
return $this->filesystem;
56-
}
57-
final public function getResponse() {
58-
return $this->response;
59-
}
60-
61-
final public function respondToRequest(Request $request) : Response
82+
final public function respondToRequest(Request $request): Response
6283
{
6384
$path = $request->getUri()->getPath();
6485
if ($this->basePath) {
6586
$path = str_replace($this->basePath, "", $path);
6687
}
67-
$path = rawurldecode($path);
68-
88+
$path = rawurldecode($path);
89+
6990
// @FIXME: The path can also come from a 'Slug' header
7091

7192
$method = $this->getRequestMethod($request);
7293

7394
$contents = $request->getBody()->getContents();
74-
95+
7596
return $this->handle($method, $path, $contents, $request);
7697
}
7798

7899
////////////////////////////// UTILITY METHODS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
79100

80-
private function getRequestMethod(Request $request) : string
101+
private function getRequestMethod(Request $request): string
81102
{
82103
$method = $request->getMethod();
83104

@@ -93,18 +114,7 @@ private function getRequestMethod(Request $request) : string
93114
return $method;
94115
}
95116

96-
public function setPubSubUrl($url) {
97-
$this->pubsub = $url;
98-
}
99-
public function setBaseUrl($url) {
100-
$this->baseUrl = $url;
101-
102-
$uri = $this->baseUrl;
103-
$serverRequest = new \Laminas\Diactoros\ServerRequest(array(),array(), $this->baseUrl);
104-
$this->basePath = $serverRequest->getUri()->getPath();
105-
}
106-
107-
private function handle(string $method, string $path, $contents, $request) : Response
117+
private function handle(string $method, string $path, $contents, $request): Response
108118
{
109119
$response = $this->response;
110120
$filesystem = $this->filesystem;
@@ -177,10 +187,10 @@ private function handle(string $method, string $path, $contents, $request) : Res
177187
$filename = $slug;
178188
} else {
179189
$filename = $this->guid();
180-
}
190+
}
181191
// FIXME: make this list complete for at least the things we'd expect (turtle, n3, jsonld, ntriples, rdf);
182192
// FIXME: if no content type was passed, we should reject the request according to the spec;
183-
193+
184194
switch ($contentType) {
185195
case "text/plain":
186196
$filename .= ".txt";
@@ -229,7 +239,7 @@ private function handle(string $method, string $path, $contents, $request) : Res
229239
return $response;
230240
}
231241

232-
private function handleSparqlUpdate(Response $response, string $path, $contents) : Response
242+
private function handleSparqlUpdate(Response $response, string $path, $contents): Response
233243
{
234244
$filesystem = $this->filesystem;
235245
$graph = new \EasyRdf_Graph();
@@ -243,7 +253,7 @@ private function handleSparqlUpdate(Response $response, string $path, $contents)
243253

244254
try {
245255
// Assuming this is in our native format, turtle
246-
$graph->parse($data, "turtle");
256+
$graph->parse($data, "turtle");
247257
// FIXME: Adding this base will allow us to parse <> entries; , $this->baseUrl . $this->basePath . $path), but that breaks the build.
248258
// FIXME: Use enums from namespace Pdsinterop\Rdf\Enum\Format instead of 'turtle'?
249259
@@ -314,7 +324,7 @@ private function handleSparqlUpdate(Response $response, string $path, $contents)
314324
return $response;
315325
}
316326

317-
private function handleCreateRequest(Response $response, string $path, $contents) : Response
327+
private function handleCreateRequest(Response $response, string $path, $contents): Response
318328
{
319329
$filesystem = $this->filesystem;
320330

@@ -336,7 +346,9 @@ private function handleCreateRequest(Response $response, string $path, $contents
336346

337347
return $response;
338348
}
339-
private function parentPath($path) {
349+
350+
private function parentPath($path)
351+
{
340352
if ($path == "/") {
341353
return "/";
342354
}
@@ -347,8 +359,8 @@ private function parentPath($path) {
347359
}
348360
return implode("/", $pathicles) . "/";
349361
}
350-
351-
private function handleCreateDirectoryRequest(Response $response, string $path) : Response
362+
363+
private function handleCreateDirectoryRequest(Response $response, string $path): Response
352364
{
353365
$filesystem = $this->filesystem;
354366
if ($filesystem->has($path) === true) {
@@ -366,12 +378,13 @@ private function handleCreateDirectoryRequest(Response $response, string $path)
366378
return $response;
367379
}
368380

369-
private function sendWebsocketUpdate($path) {
381+
private function sendWebsocketUpdate($path)
382+
{
370383
$pubsub = $this->pubsub;
371384
if (!$pubsub) {
372385
return; // no pubsub server available, don't even try;
373386
}
374-
387+
375388
$pubsub = str_replace("https://", "ws://", $pubsub);
376389
$pubsub = str_replace("http://", "ws://", $pubsub);
377390

@@ -389,8 +402,8 @@ private function sendWebsocketUpdate($path) {
389402
$client->send("pub $baseUrl$path\n");
390403
}
391404
}
392-
393-
private function handleDeleteRequest(Response $response, string $path, $contents) : Response
405+
406+
private function handleDeleteRequest(Response $response, string $path, $contents): Response
394407
{
395408
$filesystem = $this->filesystem;
396409

@@ -429,7 +442,7 @@ private function handleDeleteRequest(Response $response, string $path, $contents
429442
return $response;
430443
}
431444

432-
private function handleUpdateRequest(Response $response, string $path, string $contents) : Response
445+
private function handleUpdateRequest(Response $response, string $path, string $contents): Response
433446
{
434447
$filesystem = $this->filesystem;
435448

@@ -448,7 +461,8 @@ private function handleUpdateRequest(Response $response, string $path, string $c
448461
return $response;
449462
}
450463

451-
private function getRequestedMimeType($accept) {
464+
private function getRequestedMimeType($accept)
465+
{
452466
// text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
453467
$mimes = explode(",", $accept);
454468
foreach ($mimes as $mime) {
@@ -461,18 +475,19 @@ private function getRequestedMimeType($accept) {
461475
return $mimeInfo;
462476
break;
463477
}
464-
}
478+
}
465479
return '';
466480
}
467-
private function handleReadRequest(Response $response, string $path, $contents, $mime='') : Response
481+
482+
private function handleReadRequest(Response $response, string $path, $contents, $mime=''): Response
468483
{
469484
$filesystem = $this->filesystem;
470485
if ($path == "/") { // FIXME: this is a patch to make it work for Solid-Nextcloud; we should be able to just list '/';
471486
$contents = $this->listDirectoryAsTurtle($path);
472487
$response->getBody()->write($contents);
473488
$response = $response->withHeader("Content-type", "text/turtle");
474489
$response = $response->withStatus(200);
475-
} else if ($filesystem->has($path) === false) {
490+
} else if ($filesystem->has($path) === false) {
476491
$message = vsprintf(self::ERROR_PATH_DOES_NOT_EXIST, [$path]);
477492
$response->getBody()->write($message);
478493
$response = $response->withStatus(404);
@@ -509,12 +524,14 @@ private function handleReadRequest(Response $response, string $path, $contents,
509524

510525
return $response;
511526
}
512-
513-
private function guid() {
527+
528+
private function guid()
529+
{
514530
return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
515531
}
516532

517-
private function listDirectoryAsTurtle($path) {
533+
private function listDirectoryAsTurtle($path)
534+
{
518535
$filesystem = $this->filesystem;
519536
if ($path == "/") {
520537
$listContents = $filesystem->listContents(".");// FIXME: this is a patch to make it work for Solid-Nextcloud; we should be able to just list '/';
@@ -523,7 +540,7 @@ private function listDirectoryAsTurtle($path) {
523540
}
524541
// CHECKME: maybe structure this data als RDF/PHP
525542
// https://www.easyrdf.org/docs/rdf-formats-php
526-
543+
527544
$name = basename($path) . ":";
528545
// turtle syntax doesn't allow labels that start with a number, so prefix it if it does;
529546
if (preg_match("/^\d/", $name)) {
@@ -535,7 +552,7 @@ private function listDirectoryAsTurtle($path) {
535552
"ldp:contains" => array()
536553
)
537554
);
538-
555+
539556
foreach ($listContents as $item) {
540557
switch($item['type']) {
541558
case "file":
@@ -547,7 +564,7 @@ private function listDirectoryAsTurtle($path) {
547564
break;
548565
case "dir":
549566
// FIXME: we have a trailing slash here to please the test suits, but it probably should also pass without it since we are a Container.
550-
$filename = "<" . rawurlencode($item['basename']) . "/>";
567+
$filename = "<" . rawurlencode($item['basename']) . "/>";
551568
$turtle[$filename] = array(
552569
"a" => array("ldp:BasicContainer", "ldp:Container", "ldp:Resource")
553570
);
@@ -573,7 +590,7 @@ private function listDirectoryAsTurtle($path) {
573590
$lines[] = "\t" . $property . " " . implode(", ", $values);
574591
}
575592
}
576-
593+
577594
$container .= implode(";\n", $lines);
578595
$container .= ".\n";
579596
}

0 commit comments

Comments
 (0)