Issues (160)

src/controller/EntityController.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * EntityController is responsible for redirecting requests to the /entity address.
5
 */
6
class EntityController extends Controller
7
{
8
    private function redirect303($url)
9
    {
10
        header("HTTP/1.1 303 See Other");
11
        header("Location: $url");
12
    }
13
14
    private function redirectREST($vocab, $uri, $targetFormat)
15
    {
16
        $baseurl = $this->getBaseHref();
17
        $vocid = $vocab->getId();
18
        $query = http_build_query(array('uri' => $uri, 'format' => $targetFormat));
19
        $url = $baseurl . "rest/v1/$vocid/data?$query";
20
        $this->redirect303($url);
21
    }
22
23
    private function redirectWeb($vocab, $uri)
24
    {
25
        $baseurl = $this->getBaseHref();
26
        $vocid = $vocab->getId();
27
        $localname = $vocab->getLocalName($uri);
28
29
        if (!$localname) {
30
            $url = $baseurl . "$vocid/";
31
        } else {
32
33
            if ($localname !== $uri && $localname === urlencode($localname)) {
34
                // the URI can be shortened
35
                $url = $baseurl . "$vocid/page/$localname";
36
            } else {
37
                // must use full URI
38
                $query = http_build_query(array('uri' => $uri));
39
                $url = $baseurl . "$vocid/page/?" . $query;
40
            }
41
        }
42
        $this->redirect303($url);
43
    }
44
45
    /**
46
     * Perform a HTTP 303 redirect to the content-negotiated URL, either the
47
     * web page for a resource or a REST API URL for retrieving its data.
48
     * @param Request $request
49
     */
50
    public function redirect($request)
51
    {
52
        /* determine parameters: URI and (optional) vocabulary */
53
        $request->setUri($request->getQueryParam('uri'));
54
        if ($request->getQueryParam('vocab')) {
55
            // vocabulary explicitly set
56
            $request->setVocab($request->getQueryParam('vocab'));
57
        } else {
58
            // guess vocabulary based on URI
59
            $vocab = $this->model->guessVocabularyFromURI($request->getUri());
60
            if ($vocab === null) {
61
                return $this->returnError('404', 'Not Found', 'Unrecognized URI ' . $request->getUri());
0 ignored issues
show
Are you sure the usage of $this->returnError('404'...' . $request->getUri()) targeting Controller::returnError() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
62
            }
63
            $request->setVocab($vocab->getId());
64
        }
65
66
        // negotiate suitable response format
67
        $restFormats = explode(' ', RestController::SUPPORTED_FORMATS);
68
        $supportedFormats = $restFormats;
69
        // add HTML as supported format (make it the first element so it becomes default)
70
        array_unshift($supportedFormats, 'text/html');
71
        // optional query parameter for forcing a specific format
72
        $requestedFormat = $request->getQueryParam('format');
73
74
        $targetFormat = $this->negotiateFormat($supportedFormats, $request->getServerConstant('HTTP_ACCEPT'), $requestedFormat);
75
76
        if (in_array($targetFormat, $restFormats)) {
77
            $this->redirectREST($request->getVocab(), $request->getUri(), $targetFormat);
78
        } else {
79
            $this->redirectWeb($request->getVocab(), $request->getUri());
80
        }
81
    }
82
}
83