Completed
Branch master (8e2732)
by Osma
04:46 queued 01:55
created

EntityController::redirectREST()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 6
nc 1
nop 3
1
<?php
2
/**
3
 * Copyright (c) 2016 Aalto University and University of Helsinki
4
 * MIT License
5
 * see LICENSE.txt for more information
6
 */
7
8
/**
9
 * EntityController is responsible for redirecting requests to the /entity address.
10
 */
11
class EntityController extends Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
    private function redirect303($url)
14
    {
15
        header("HTTP/1.1 303 See Other");
16
        header("Location: $url");
17
    }
18
19
    private function redirectREST($vocab, $uri, $targetFormat)
20
    {
21
        $baseurl = $this->getBaseHref();
22
        $vocid = $vocab->getId();
23
        $query = http_build_query(array('uri'=>$uri, 'format'=>$targetFormat));
24
        $url = $baseurl . "rest/v1/$vocid/data?$query";
25
        $this->redirect303($url);
26
    }
27
    
28
    private function redirectWeb($vocab, $uri)
29
    {
30
        $baseurl = $this->getBaseHref();
31
        $vocid = $vocab->getId();
32
        $localname = $vocab->getLocalName($uri);
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
        $this->redirect303($url);
42
    }
43
44
    /**
45
     * Perform a HTTP 303 redirect to the content-negotiated URL, either the
46
     * web page for a resource or a REST API URL for retrieving its data.
47
     * @param Request $request
48
     */
49
    public function redirect($request) {
50
        /* determine parameters: URI and (optional) vocabulary */
51
        $request->setUri($request->getQueryParam('uri'));
52
        if ($request->getQueryParam('vocab')) {
53
            // vocabulary explicitly set
54
            $request->setVocab($request->getQueryParam('vocab'));
55
        } else {
56
            // guess vocabulary based on URI
57
            $vocab = $this->model->guessVocabularyFromURI($request->getUri());
58
            $request->setVocab($vocab->getId());
59
        }
60
61
        // negotiate suitable response format
62
        $restFormats = explode(' ', RestController::SUPPORTED_FORMATS);
63
        $supportedFormats = $restFormats;
64
        // add HTML as supported format (make it the first element so it becomes default)
65
        array_unshift($supportedFormats, 'text/html');
66
        // optional query parameter for forcing a specific format
67
        $requestedFormat = $request->getQueryParam('format');
68
69
        $targetFormat = $this->negotiateFormat($supportedFormats, $request->getServerConstant('HTTP_ACCEPT'), $requestedFormat);
70
        
71
        if (in_array($targetFormat, $restFormats)) {
72
            $this->redirectREST($request->getVocab(), $request->getUri(), $targetFormat);
73
        } else {
74
            $this->redirectWeb($request->getVocab(), $request->getUri());
75
        }
76
    }
77
}
78